Git Problems

Solutions to Common Issues

How do you undo a Git merge?

It happens to the best of us. You’ve just made code changes, committed them, and merged those changes into your main branch. Le gasp! You realize after the fact that 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.

We’re going to start by showing how to undo a Git merge using the cross-platform GitKraken Git GUI 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 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 Git client is extremely simple and visually intuitive because 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 central graph in GitKraken, simply double-click the branch where your changes were mistakenly merged. This will checkout the Git branch.

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.

Now, if you have already pushed the merged changes you want to undo to your remote repository, you can 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.

Once the new commit has been made to revert your merged changes, you can push the commit to the remote repository by selecting Push from the context menu.

GitTip: Get the full scoop on how to revert a commit in Git and how you can safely make modifications to your history.

If you’re just getting started with Git, undoing merges and reverting commits can sound scary, but GitKraken makes complex Git processes easy and safe.

How do you undo a Git merge in the command line?

Unlike in GitKraken, undoing a Git merge in the CLI can get a little messy if you’ve already pushed your changes to your remote. The process can also get complicated if new changes were added to the target branch since you had realized the need to undo your merge. The GitKraken Git GUI provides detailed visual context about your repo’s history in a clear layout, making it easier to figure out when the merge occurred and allowing you to quickly undo it without risking further mistakes. Make the seemingly complex process of undoing a merge easier and safer: download GitKraken now – it’s free!

In this undo Git merge example, we will go over the basic steps of how to undo a Git merge, as well as some additional steps to account for more complex situations.

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 in the process, you will need to verify that your merged commit was not already pushed to your remote. If this is the case, you will 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, you will need to make 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 Git client – download free for Windows, Mac, and Linux.

Additional Resources

Make Git Easier, Safer &
More Powerful

with GitKraken