How can I completely remove a file from a git repository?

Git

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid XView Question on Stackoverflow
Solution 1 - GitmipadiView Answer on Stackoverflow
Solution 2 - GitnachoparkerView Answer on Stackoverflow