[Git] Purging History
REPO COPY
Now you've done it. You accidentally put the company's master password into one of your files. You're beginning to panic because you know that it's pretty hard to delete something forever in git. That's what you really need to do, though, or you're in big trouble. You're going to need to purge the history. First, though, make a copy of your repo in case you mess it up. Make a copy of the poodles
repository, and name it whatever you want.
git clone poodles poodles-backup
TREE FILTER
Luckily for you, the password you accidentally committed was in the master_password.txt
file. Use the filter-branch
command and remove this file from all of your commits.
git filter-branch --tree-filter 'rm -f master_password.txt'
INDEX FILTER
This repository is so large, using --tree-filter
is going to take all day. Use --index-filter
to remove master_password.txt
instead. Remember, --index-filter
will need a command that works on the staging area, which is going to be some sort of git command.
git filter-branch --index-filter 'git rm --cached --ignore-unmatch master_password.txt'
Because --index-filter run on the staging area, so we need to add 'git' before the 'rm -f'.
FORCE
You just realized that you also have a master_username.txt
file that should probably be removed too, just to be safe. Use either technique you have learned to remove this file. Don't forget, you have a history backup from the first time you used filter-branch
. Use the correct option to force git to overwrite it.
git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch master_username.txt'
PRUNING
Great! Now we're getting somewhere. There is one more problem though, you realize that some of the commits don't contain anything anymore, since you removed the password file that they referenced. You should probably use the --prune-empty
option do something to clean these out.
git filter-branch -f --prune-empty -- --all