- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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.
- Related Articles
- What is merge conflict in Git? How to handle merge conflicts?
- How to undo a faulty merge with reset command in Git?
- How to undo a faulty merge with revert command in Git?
- What is a fast-forward merge in Git?
- What is 3-way merge or merge commit in Git?
- Abort in C#
- How to tag a commit in git?
- How to compare two branches in Git?
- How to Install Git in Vs Code?
- How to ignore a previously committed file in Git repository?
- How to Install Git on Linux
- How to install Git On Mac?
- How to change the default configuration in Git?
- exit(), abort() and assert() in C/C++
- How to Merge Elements in a Python Sequence?
