Abort/Cancel Merge In Git

As a distributed source code management system Git provides the ability to merge different branches and development histories. During the merge, the same file can be changed by multiple developers where merging them may create conflicts. The best way is to solve the merge conflict but in some cases, we may need to abort or cancel the merge in order to solve conflicts that prevent commits.

Merge Conflict

The merge conflict can occur in different ways. Generally, the “You are in the middle of a conflicted merge.” error information is displayed which prevents the commit.

unmerged:   main.c

You are in the middle of a conflicted merge.

List Merge Conflicts and Details

Before canceling or aborting a merge we can list the conflict and related details like modified or conflicted files etc.

$ git status
# On branch contact-form
# You have unmerged paths.
#   (fix conflicts and run "git commit")
# Unmerged paths:
#   (use "git add <file>..." to mark resolution)
#       both modified:   main.c
no changes added to commit (use "git add" and/or "git commit -a")

Cancel Merge with “git reset”

The git reset or git-reset command is used to reset current changes and revert back to the HEAD version. Keep in mind that the following operation can not be reverted back. All changes made by the latest commit are reset.

$ git reset --har HEAD

Cancel Merge with “git merge”

The git merge or git-merge command also provides the ability to cancel or abort the merge operation. This command is safer than the git-reset as the git-merge only cancels the current merge and do not revert back or reset changes.

$ git merge --abort

Leave a Comment