Monday, February 01, 2016

Purging a file from a git repo


Step 1) Clone the repo.

Step 2) Remove the file from current repo.
$ git rm Documentation/master/rc.slack
$ git commit -m 'remove unneeded file'
$ git push origin uno_v0



Step 3) Purge the file


You need to run this command from the toplevel of the working tree.


$ git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch Documentation/master/rc.slack' --prune-empty --tag-name-filter cat -- --all




Note: the output:
WARNING: Ref 'refs/remotes/origin/uno_v0' is unchanged


That is why we needed to `git rm ...` and `git commit ...` and `git push ...` in Step 2.

Step 4) push the changes to the remote


$ git push -f --all origin


This will remove the file from all branches in the remote. However, if other people have cloned this repo and have a local copy of it, they either need to throw away the old copy and re-clone or rebase all of their branches to the new history, otherwise they will bring back the file when they merge/do pull requests.