It happens to the best of us. You’ve just merged changes into your main branch when: Le gasp! You realize you shouldn’t have done that.
When this happens, you’ll need a way to get your main branch back to its previous state. And thankfully, Git has a tool for that.
Let’s first show how to undo a Git merge using the cross-platform GitKraken Desktop, before reviewing how to undo a merge in Git using the command line.
Have you made an improper merge in the past, and weren’t sure how to fix it? GitKraken Desktop (a Git client) makes undoing merges and resolving conflicts intuitive, so you have less to worry about.
How do you undo a Git merge using GitKraken?
Undoing a Git merge using the GitKraken Desktop is simple and visually intuitive. When you’re able to view every branch in your repo in the same UI, it’s easy to identify when an improper merge occurred and undo it with a few clicks. Download the #1 free Git GUI now and keep reading to learn how to undo a Git merge.
From the Commit Graph in GitKraken, double-click the branch where your changes were mistakenly merged. This will checkout the Git branch.
![](https://www.gitkraken.com/wp-content/uploads/2021/05/checkout-git-branch-merged-changes-gk-1024x220.jpg)
Find the previous commit in the graph, or another commit you wish to reset to. You will then select Reset <branch name> to this commit > Hard - discard all changes from the context menu.
![](https://www.gitkraken.com/wp-content/uploads/2021/05/reset-hard-gitkraken-1024x367.jpg)
If you already pushed the merged changes you want to undo to your remote repository, right-click on the merge commit and select Revert commit from the context menu.
You will then be asked if you want to immediately create a commit.
![](https://www.gitkraken.com/wp-content/uploads/2021/05/revert-commit-gk-1024x309.jpg)
Once the new commit has been made to revert your merged changes, push the commit to the remote repository by selecting Push from the context menu.
![](https://www.gitkraken.com/wp-content/uploads/2021/05/push-revert-commit-gk-1024x431.jpg)
GitTip: Get the full scoop on how to revert a commit in Git and how to safely make modifications to your history.
Undoing merges and reverting commits can sound scary, but GitKraken Desktop makes complex Git processes easy and safe.
How do you undo a Git merge in the command line?
Unlike in GitKraken Desktop, undoing a Git merge in the CLI can get messy if you’ve already pushed your changes to your remote. The process can be complicated if new changes were added to the target branch since realizing the need to undo your merge.
To undo a Git merge in the CLI, you will start by checking out the branch where you have merged your changes into.
git checkout <branch-name>
From here, you will need to obtain the ref of the commit you need to reset the branch back. You will do this using git reflog.
git reflog show --all
After running git reflog, you will identify the commit you want to return to and copy the commit SHA.
At this point, verify that your merged commit was not already pushed to your remote. If this is the case, move forward with the git reset command.
git reset --hard <merge-commit-sha>
GitTip: If you happen to have any uncommitted changes, be sure to use the Git stash command before running Git reset.
How do you undo a Git merge after pushing the changes?
If you have already pushed the merge commit to the remote repo, consider making a new commit that reverts the changes.
git revert -m 1 <merge-commit-sha>
This will create a new commit that reverses the changes from the merge commit.
The process to undo a Git merge is far less complicated and takes fewer steps using the legendary GitKraken Desktop – download free for Windows, Mac, and Linux.