Git Undo
Oct 05 2014
2 minutes read

Here are some useful git commands that helps to undo changes you knowingly or accidentally(my case) committed to a repo.

git commit - -ammend

While this may be a very known feature of git, I still would add a note about it here for my own reference. Git has this awesome feature where you can amend you last commit. It comes pretty handy when you accidentally forgot to add something to you last commit.

All you have to do is:

git add <file>
git commit --amend

You can visit here for more info about this command.

git reset

This command as a very neat bzr counterpart called uncommit. While reset can accomplish what uncommit did it has many more features. For simply undoing the last commit you can do:

git reset --soft HEAD^

To also remove the changes in the last commit you can do:

git reset --hard HEAD^

This made me wonder what this command does(I never used this command before)

git reset HEAD^

Apparently this command helps you undo your last commit and everything you’d staged. Also if you had already pushed your code to some remote repository then you will have to force the next push because one of the commits was removed( and most probably one or more would have been added before push) which may lead the git to believe that the remote and local branches have diverged. Use -f (to force the push) flag with a little care, it may prove deleterious at times.

More info about this command can be viewed here


Back to posts