Search code examples
gitrevert

How to delete corrupt file in git commit


I accidentally committed a file to my git repo that was corrupt. The corruption happened when I was working on my laptop and the batteries died down. I didn't recognize at once that this ended up in a file which consisted of weird symbols only. However, I committed it to my repo.

Now I managed to revert to the previous non-corrupt version by using git revert HEAD^ and I have a non-corrupted version of the file. So far so good.

However, the log shows me that the corrupted file is still in the tree of my repo. My question now: Is this problematic, or can I forget about it? Is there a way to erase that corrupt commit/file out of the repo?


Solution

  • if you have not pushed the changes, git revert is probably not what you want. It makes a new commit that reverses the changes made.

    Use git reset --hard <last good commit> to remove the bad commit. You can address this commit with a HEAD reference as you did, but that will not adjust the commits in the branch. If you reference the last known good commit with the branch name, such as git reset --hard <branch>~2 to reference 2 commits prior to the current one.