How To Abort A Merge In Git?

Tim Mouskhelichvili
Tim Mouskhelichvili
3 minutes to read

While working on a git repository, you will always need to merge different branches into other ones. If you have many people working on the same repository, oftentimes trying to merge branches will result in merge conflicts. This brings the question of how to abort a merge with conflicts in git.

To abort a merge with conflicts in git you can either:

  • Use the git merge --abort command.
  • Use the git reset --hard HEAD command.
  • Utilize the git reset --merge command.

In this article, I will go over, in detail, why does a merge conflict happen, as well as explain different ways of canceling a merge.

Let’s get to it 😎.

git abort merge

First, we need to understand why do merge conflicts happen.

Why do merge conflicts happen?

Merge conflicts can happen when merging, but also when pulling from a branch.

Typically, merge conflicts happen, when your current branch and the branch that you want to merge have diverged. This means that some commits present on your current branch are not present on the other branch, and vice-versa.

Branch divergence is very common when two or more developers are working on the same repository and modifying the same files.

How do merge conflicts happen?

When git tries to merge two branches, it can only apply unambiguous differences. This means that git can only apply the changes if only one side changed a particular part of the code.

However, if both sides made changes to the same part of the code, git WILL NOT apply the changes. It will create conflict markers on the conflicted files. This situation is also called a merge conflict.

You will need to resolve those merge conflicts either by:

  • using git commands to resolve the conflicts.
  • using an external visual merge tool.
  • aborting the merge.

Why might we want to abort a merge?

You need this option because, sometimes, you want to go back to the old state before the merge happened. By doing that you will have all of your files unchanged and will be able to do the necessary changes from the beginning.

Note: If you are interested in learning how to abort a Git rebase, I've also written a guide.

How to cancel a merge?

When it comes to aborting a merge with conflicts in git you have three options.

1. Use the git merge --abort command.

bashgit merge --abort

This command is the default solution to abort a merge.

2. Use the git reset --hard HEAD command.

bashgit reset --hard HEAD

This command will reset the HEAD to the last valid commit, which in this case is the commit before the merge.

3. Use the git reset --merge command.

bashgit reset --merge

This is the equivalent of using git merge --abort when a MERGE_HEAD is present.

To verify that we canceled a merge successfully, use the git status command.

Final thoughts

Here you go.

Now you understand how to cancel a merge with conflicts in git and know different ways to archive this goal. Every developer must have this knowledge whether you are a front-end, back-end, or full-stack developer.

git abort merge

I have written more tutorials on git if you are interested in learning more about it and its commands.

If you have any questions please ask them in the comments. It will be my pleasure to answer them to the best of my abilities.

Thank you for reading through this article, please share it with your fellow coders.

Comments (0)
Reply to: