Pirun Seng

Ruby on Rails Developer

Undo Git Merge


Git is awesome and that I’ve used it along with my career, software development, so far.

I sometimes get confused doing git merge between branches. When it is a tough time preparing to release my feature to test server in which I have to merge the latest master branch, into my current working branch, let say update-layout branch.

Unexpectedly, I have merged update-layout into master. Actually, I want to merge master into update-layout.

Luckily, I haven’t pushed the master yet so that I can revert/undo merge safely.

To undo the merge, it is as simply as the following:

  • Stay on master branch by git checkout master
  • Type git log on the terminal to see the commit histories.
  • Find the latest commit of master branch we want to revert to.
  • Copy the commit sha, e.g. dbf0e49.
  • Back to terminal and type git reset --hard dbf0e49
  • It’s done! We’re on the latest commit as we want to revert/undo merge.

If we’re ready to merge update-layout into master or redo merge, it can be done easily for just do the merge again.

Happy with Git!