Imagine a scenario where you have a git repo with 2 branches;
master, the production-ready branch and
dev, the branch where all the development occurs.
Now imagine that you accidentally made a commit on
master, when really it should have been on
dev. If you have not yet pushed to a remote repository (like Github), you can undo that commit using
git reset like so:
git reset --soft HEAD~1
This will bring your repository back to the state it was in right before you did your
git commit. Now you can switch to your
dev branch and re-commit the changes in the right place.
--soft option tells git to leave your index (or “staging area”) and your working tree alone. If you were to run this same command with
--hard it would trash all your local changes. This is fine if you want to throw all your work away, but if the work is good, just the commit was bad, then use
HEAD~1 just means “the latest commit’s parent”. It could also be written as
This is all well and good, but what if you had
git pushed right after doing the erroneous commit? If you just try the steps outlined above, and then try and push to your remote repo, you will get an error because the tip of your local repo is behind that of the remote and it will reject your push.
This is where you need to use
git revert HEAD
This command essentially says, “I want to create a new commit that undoes the commit pointed to by
HEAD”. Once the command has been executed, it creates a new commit which you can push back to your remote repo which will effectively create a patch which undoes all the changes in the last commit.