How to abort a merge conflict in Git?


When doing a merge, you may run into one or more conflicts. Now what if we are not quite ready to handle this conflict yet. Perhaps you have several conflicts and you don't have enough time to spend on resolving these conflicts. In situations like this we can easily go back to the state before we started the merge.

To abort the merge, we can use the following command

$ git merge --abort

Note that if we execute this command after resolving some conflicts and staging the changes, then these changes would not be saved. Once the command is fired, we are back to the clean state i.e., the state before we started the merge.

We can understand this from the below diagram. There are two branches, master and feature, and each one is operating on the same file. When we merge the feature branch with the master branch, we will end up with a merge conflict error. Now if we abort the merge conflict we will get back to the state before the conflict as shown in the diagram.

Example

Let us see this in action to understand how to abort a merge conflict.

Step 1 − Create a repository with initial commit with hello.txt file.

$ git init
Initialized empty Git repository in E:/tut_repo/.git/

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo hello>hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -m 'hello.txt'
[master (root-commit) d861f5f] hello
1 file changed, 1 insertion(+)
create mode 100644 hello.txt

Step 2 − Create a new branch feature. Switch to feature branch and create a new commit by editing second line in hello.txt file

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git branch feature

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git switch feature
Switched to branch 'feature'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ echo hello feature >> hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git commit -m 'hello feature'
[feature 4b2c833] hello feature
1 file changed, 1 insertion(+)

Step 3 − Switch to master branch and perform a new commit by adding a new line to hello.txt.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature)
$ git switch master
Switched to branch 'master'

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ cat hello.txt
hello

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ echo hello master>>hello.txt

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git add .

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git commit -m 'hello master'
[master 6b1e9a4] hello master
1 file changed, 1 insertion(+)

Step 4 − We will now merge changes from feature branch to the master branch.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git merge feature
Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master|MERGING)

The output shows that the branch is in an intermediate state of merging as the automatic merging failed due to conflict.

Step 5 − Now we decide to abort the merge conflict. Before aborting we can check the status of the repository using git status.

$$ git status -s
UU hello.txt

The UU status shows the file hello.txt is in the unmerged state in the working directory and the staging area.

Step 6 − Finally let’s abort the conflict

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master|MERGING)
$ git merge --abort

$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master)
$ git status
On branch master
nothing to commit, working tree clean

From the output it is clear that after aborting the merge (master|MERGING) has changed to (master).

Also, if we check git status it will indicate that the working tree is clean.

Updated on: 30-Apr-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements