I noticed recently that the project files my text editors use (along with some other junk) got added the git repository for the project. Since they aren't actually part of the project, I'd like to remove them, but git rm
doesnt remove the old versions from the repository, and I couldnt find anything else that looks promising.

- 3,998
- 3
- 29
- 32
-
5partial duplicate, definitely covered by previous questions: http://stackoverflow.com/questions/307828/git-remove-file-accidentally-added-to-the-repository http://stackoverflow.com/questions/1216733/remove-a-directory-permanently-from-git – Cascabel Aug 11 '10 at 13:25
3 Answers
The tool you want is git filter-branch
. Its usage is described here, but basically:
$ git filter-branch --tree-filter 'rm -f my_file' HEAD
will remove "my_file" from every commit.
Notice that this rewrites every commit, so if you push into a remote repository, you have to (a) force the update, and (b) everyone else who pulled from you will now have duplicate commits (since you rewrote the history), as described on the git rebase
man page.

- 398,885
- 90
- 523
- 479
-
20I recommend to add `--prune-empty` so any empty commits generated as a result will be removed too. – fnkr Aug 28 '14 at 07:21
-
How does this work on a repository with many files with the same name in different directories ? Does it delete all files with the same name ? – Nov 21 '16 at 10:01
-
2
-
1
-
1@mipadi, what if I deleted these files locally, long ago? (and committed and pushed these deletions) How can I delete the files from the repository in this case? – mannyglover Nov 15 '18 at 18:47
-
Everyone should remember that this WILL DELETE the files from your current working directory as well. This has been obvious to me every time I've used this command, except for this time. – Kyle Aug 06 '19 at 20:59
-
1
-
-
It doesn't work for me, I get: + CategoryInfo : NotSpecified: (WARNING: Ref 'r...p' is unchanged:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError – Giox Apr 21 '21 at 11:40
-
WARNING: this can be very long operation ! I don t understand that the first time i have runned that command ... – rdhainaut May 03 '21 at 12:37
-
I had to add "if" into a filter command: ```git filter-branch --tree-filter 'if [ -e DIR ];then rm -rf DIR ; fi' HEAD```; without "if" it reported "Permission denied" – Andrzej Martyna Sep 13 '22 at 13:02
-
actually not sure about adding "if" so take my advice with a grain of salt (this is because I sometimes get "Permission denied" even with the "if", not sure why) – Andrzej Martyna Sep 13 '22 at 14:31
This is what git filter-branch
is for, but beware that your repo history will change, and the commit hashes will be different after history rewrite.
If you also want to free the space, I recommend that you use git forget-blob
, because git filter-branch
alone will not make git forget your file, as it may still be referenced by remotes, reflog, tags and such.
git forget-blob main.c.swp
You can get more information here

- 1,678
- 18
- 14
It's now recommended to use git-filter-repo instead. Running git-filter-branch
actually prints:
WARNING: git-filter-branch has a glut of gotchas generating mangled history

- 16,634
- 25
- 104
- 157