Git

Git Abort Merge Example

In this post, we present a Git Abort Merge Example.

1. Introduction

Merging branches is an important operation when working with Version Control Systems (VCSs). One feature that makes Git standout among other VCSs is its branching mechanism. The mechanism used by Git to create and manage branches is both lightweight and efficient in comparison to other VCSs. Because branching and merging are very fast in Git, it is recommended that you merge your branches often.

It is not uncommon to find that a branch has diverged too much from the mainline. When this happens, conflicts can occur while attempting to merge the branch back into the master branch (the default name for the main branch). When this happens, you have two options:

  • You can resolve the conflicts and resume the merge process with the “continue” option.
  • You can cancel the merge process and try to restore the branches to their pre-merge state with the “abort” option.

In this article, we will demonstrate how to use the merge abort command if we have merge conflicts.

Warning: It is strongly recommended that you commit any changes before attempting to merge branches. Failure to do so may result in lost changes if you use the merge command with the “abort” option.

1.1 Tools Used in this Example

  • Git 2.17

Git downloads are available here: https://git-scm.com/downloads.

2. Git Abort Merge Example

In this example, we will make changes to the same file in two different branches and use the Git merge operation to attempt to merge the branches. We will then cancel the merge operation using the abort option.

2.1 Download and Extract the Sample Project

First, download the “Merge Abort Example” archive from the Download section and extract it in an empty directory of your choice.

2.2 Compare the Branches using Git diff

The sample project has two branches. To list the branches, run the following command in the “git-abort-example” directory:

$ git branch

Git branch Command Output

* master
  spanish

The * next to master tells us that we are in the master branch. If you are in the “spanish” branch, run git checkout master.

At this point, the “spanish” branch and the “master” branch are pointing to different commits. You can verify this by running the Git log command.

$ git log --oneline --all

Git log Command Output

9ae2efa (spanish) Changed greeting
30dad54 (HEAD -> master) Intial commit of project

Let’s compare the branches. Run the git “diff” command:

$ git diff spanish

Git diff Command Output

diff --git a/HelloWorld.java b/HelloWorld.java
index 4d6d395..277a287 100644
--- a/HelloWorld.java
+++ b/HelloWorld.java
@@ -1,5 +1,5 @@
 public class HelloWorld {
        public static void main(String[] args){
-               System.out.println("Hola Mundo!");
+               System.out.println("Hello World!");
        }
-}
+}

Hit q to quit. On Windows, you may need to hit Control + \ to quit.

The output shows that there is a difference in the greeting of HelloWorld.java.

2.3 Edit HelloWorld.java in the Master Branch and Commit the Change

While in the “master” branch, edit the greeting in HelloWorld.java using your favorite text editor.

HelloWorld.java

public class HelloWorld {
        public static void main(String[] args){
                System.out.println("Bonjur tout le monde!");
        }
}

Next, commit the change with the Git commit command:

$ git commit -a -m 'Changed to French greeting'

Git commit Command Output

[master cf3b996] Changed to French greeting
 1 file changed, 2 insertions(+), 2 deletions(-)

Let’s compare the branches again. Run the git “diff” command:

$ git diff spanish

Git diff Command Output

diff --git a/HelloWorld.java b/HelloWorld.java
index 4d6d395..277a287 100644
--- a/HelloWorld.java
+++ b/HelloWorld.java
@@ -1,5 +1,5 @@
 public class HelloWorld {
        public static void main(String[] args){
-               System.out.println("Hola Mundo!");
+               System.out.println("Bonjur tout le monde!");
        }
-}
+}

Hit q to quit. On Windows, you may need to hit Control + \ to quit.

The output shows that there is a difference in the greeting of HelloWorld.java. Let’s try to merge the two branches.

2.4 Merge the Branches

We would like to merge the “spanish” branch into the master branch. While in the master branch, run the following command:

$ git merge spanish

Git merge Command Output

Auto-merging HelloWorld.java
CONFLICT (content): Merge conflict in HelloWorld.java
Automatic merge failed; fix conflicts and then commit the result.

So what happened? The Git merge command will automatically attempt to incorporate changes from both branches if it can do so cleanly. However, in our situation the changes are in the same area of the file, causing the conflict. You can see this by viewing the file.

$ cat HelloWorld.java

Output

public class HelloWorld {
        public static void main(String[] args){
<<<<<< HEAD
                System.out.println("Bonjur tout le monde!");
=======
                System.out.println("Hola Mundo!");
>>>>>> spanish
        }
}

The Git merge command added markers to help show you where the conflict occurred. The top part (marked with <<<<<< HEAD) shows the file content in the current branch while the bottom part (between ======= and >>>>>> spanish) shows the file content in the “spanish” branch.

At this point, you could try to resolve the conflict yourself by running the mergetool command (you will need to configure a merge tool in Git) and then complete the merge process with merge --continue. (There are other tools at your disposal for resolving conflicts. The topic is beyond the scope of this example.) You can also decide not to continue and cancel the merge with the merge --abort command. Let us do the latter. Run the following command:

$ git merge --abort

This will back out the merge and reset the repository back to its pre-merged state. (Note that if you have uncommitted changes in your branches, this may not be possible.) You can verify this by viewing the file.

$ cat HelloWorld.java
public class HelloWorld {
        public static void main(String[] args){
                System.out.println("Bonjur tout le monde!");
        }
}

3. Summary

In this post, we demonstrated how to abort a merge operation in Git when there is a merge conflict.

4. Download the Source Code

That was the Git Abort Merge example.

Download
You can download the full source code of this example here: Merge Abort Example

Gilbert Lopez

Gilbert Lopez is an application developer and systems integration developer with experience building business solutions for large and medium-sized companies. He has worked on many Java EE projects. His roles have included lead developer, systems analyst, business analyst and consultant. Gilbert graduated from California State University in Los Angeles with a Bachelor of Science degree in Business.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button