RudyGems

Matthew Rudy Jacobs
see me swim
Github
see some of my code
Thought Sauce
Hire me in Hong Kong

How to delete a file from Git History

I always think about this problem;

What happens if I accidentally commit my password into a git repository?

Surely it’ll be there forever?

Or

Oh man… I just committed a 700mb video file into the repository

It’s going to take forever to deploy…

I’ve always known “get rebase” will probably be the answer

Luckily ProGit knows exactly what to do. 

git filter-branch —tree-filter ‘some-bash-command’ HEAD

So here’s my example of how to use it.

I’ve accidentally added a picture of my bum to this rep.

$ git show 84c344
commit 84c344c1874736a6d063ed45abe21e20815525b9
Author: Matthew Rudy Jacobs <MatthewRudyJacobs@gmail.com>
Date:   Wed Sep 15 18:25:35 2010 +0800

  add my bum

diff —git a/mybum.jpg b/mybum.jpg
new file mode 100644
index 0000000..018be38
—- /dev/null
+++ b/mybum.jpg
@@ -0,0 +1 @@
+a picture of my bum

so let’s try and wipe it from history.

$ git filter-branch —tree-filter ‘rm -f mybum.jpg’ HEAD
Rewrite 76a50a3b1b3263d84517c8e6241ec0bf2bf36fdc (4/4)
Ref ‘refs/heads/master’ was rewritten

now commit 84c344 is still there, but it is not in our branch.
Looking at git log, I can see it’s been rewritten to f59366

$ git show f59366
commit f59366420a8b753de5a0d2812e0abbfb95fccfdf
Author: Matthew Rudy Jacobs <MatthewRudyJacobs@gmail.com>
Date:   Wed Sep 15 18:25:35 2010 +0800

    add my bum

so my bum is no longer there. In the working tree, or in history.

Right now DON’T git pull it will merge your commits with the original commits.

You’ll have achieved nothing (except doubling the number of commits)

I don’t have an elegant solution… but this seems the only way.

delete all remote repositories

and push to them again

I’m sure there’s a better way… but I haven’t found it.

Here’s my example repo http://github.com/matthewrudy/git-rebase-test

go play!