How to remove a file from a repo (.gitignore
by itself is not enough)
Most likely, the file that is being overwritten was added to the repository before it was added to the .gitignore
file.
If the file was added to the repo in an older commit, you will need to delete the file, then add the deletion to the index, and make a new commit. This is counterintuitive at first, because you will delete the file, and then git add <file that was just deleted>
, but you just need to realize that you are not adding the file itself to the index to be committed, you are only adding the new changes to this file to the index to be committed. Those changes can be adding a new file, changing an existing file, or removing that file from the repo.
Once you commit the deletion, any commit from that point forward will not have the file, and if you readd the file it will be ignored because of the .gitignore
file.
Why your file keeps getting overwritten
The key here is that adding a file to .gitignore
will not remove a file from the repo by itself. It will only keep git from tracking any new changes to the file. The result is that the file in the repo never changes, so even if you change it locally, all commits in the repo will have the old version of the file, so as soon as you checkout any commit, it will overwrite your changes with the old version of the file.