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?
1 Answer
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 thisgit-rm
command. This removes any file namedpasswords.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:
A
\
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