Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I accidentally committed some sensitive information to a public git repo (yikes!). Is there any way I can permanently remove all history between a certain date range? Should I just delete the entire repo and start fresh?

share|improve this question
up vote 4 down vote accepted

The standard way of doing this is with git-filter-branch. This lets you walk through a history of commits and rewrite each one in some way. In your case, let's say you accidentally committed passwords.txt and need to remove it from your history.

The command you would run might look something like this:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch passwords.txt' HEAD

Piece by piece:

  • git filter-branch : Rewrite history.

  • --index-filter : For each commit, we want to change the index in some way then re-commit it.

  • 'git rm --cached --ignore-unmatch passwords.txt' : For each commit, change the index by running this git-rm command. This removes any file named passwords.txt from the index.

  • HEAD : Rewrite the history of the branch currently checked out.

When finished, you'll have a brand new commit checked out with identical history except for the lack of passwords.txt. Since you've rewritten history, you'll need to warn anyone sharing the repository from you that they must recover from your upstream rebase.

For example, if your history looked like this, and commits B-C were tainted:

A ---- B ---- C ---- D ---- E

When you're finished, your history will now look like this:

  B' ---- C' ---- D' ---- E' 

Notice that D and E have been re-written to D' and E'. They might point to the same trees, but since history changed, they will have new commit IDs and are considered new commits as far as Git is concerned.

More information about using git-filter-branch can be found in the "Rewriting History" section of the Pro Git book

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .