revert a file in git and removing it from history

Viewed 44

Is there a way to revert a file from a specific commit and also remove from the history that it was ever changed?

For instance, if the file is named helpme.txt and the commit was c5ggjj5. How can i revert it back to the way it was before the change and have it never show up in the history that it was changed?

1 Answers

It can be done like this:

git rebase -i c5ggjj5~
# the first revision should be the one you want to modify
# mark that revision with edit or e
# save and exit
# rebase will start and will stop on that specific revision
git rm --cached helpme.txt #keeping it in the working tree, just in case
git commit --amend --no-edit
git rebase --continue

And if the file was not touched by any other later revisions, rebase should run without issues. Warning: you are rewriting history which has implications.

Related