Use gimerge -- branch conflict handling

Version control system is responsible for managing submissions from multiple submitters (usually developers). Sometimes multiple developers may edit the same part of the content. Once developer A edits what developer B is editing, A conflict will arise. In order to reduce the probability of conflict, developers will work in separate branches. The main responsibility of the git merge command is to integrate different branches and resolve conflicts.

Understanding merge conflicts

Merging and conflict are common scenarios in using GIT. Conflicts in other version control tools can be very time-consuming. Git makes merging easier. Most of the time, GIT will figure out how to automatically integrate new changes.

Conflict usually comes from two different developers changing the same line of content in the same file, or one developer deleting the file being modified by another developer. In such scenarios, GIT cannot automatically determine whose changes should be adopted. When a conflict occurs, it will only affect the developers performing the merge operation, while other team members will not be affected. Git marks the conflicting files and stops the merge process. Next, developers need to deal with conflicts by themselves.

Type of Merge conflict

Entering the conflict state occurs at two points in the merge process. One is when the merge starts, and the other is during the merge process. The following contents are about how to solve these two different scenarios.

Conflict at the beginning of the merge

Git interrupts the merge operation when a file in the working directory or in the staging area contains changes. Git does this because git believes that these uncommitted changes will be overwritten by the merge operation. When this happens, it is not because the change will conflict with the submission of other developers, but because the merge itself will conflict with the local modification. At this time, you need to use git stash, git checkout,git commit or git reset commands to make the local warehouse in a stable state. The failure at the beginning of the merge operation will prompt the following error message on the command line:

error: Entry '<fileName>' not uptodate. Cannot merge. (Changes in working directory)

Conflict during consolidation

A conflict in the merge process means that your local branch conflicts with the branch that is merging. In other words, your local code conflicts with the code of other developers. Git will try to merge files from different branches, but if there is a real conflict, it will still hand over the task of manual merging to you. In this scenario, GIT will leave the following error message on the command line:

error: Entry '<fileName>' would be overwritten by merge. Cannot merge. (Changes in staging area)

Create a merge conflict

In order to really understand the merge conflict, the following content will manually simulate the creation of a conflict and view and resolve the conflict later. The following code requires you to use the command line Git tool of * nix system to create this simulated conflict.

$ mkdir git-merge-test
$ cd git-merge-test
$ git init .
$ echo "this is some content to mess with" > merge.txt
$ git add merge.txt
$ git commit -am"we are commiting the inital content"
[main (root-commit) d48e74c] we are commiting the inital content
1 file changed, 1 insertion(+)
create mode 100644 merge.txt

The above code actually does the following things:

  • Create a directory called Git merge test, enter this directory, and then initialize a Git repository
  • Create a merge with whatever content Txt file
  • Add merge Txt file to the warehouse and then submit it

At this point, we have a new Git repository, which contains the main branch and a content merge Txt file. Next, let's create another branch as the conflicting branch.

$ git checkout -b new_branch_to_merge_later
$ echo "totally different content to merge later" > merge.txt
$ git commit -am"edited the content of merge.txt to cause a conflict"
[new_branch_to_merge_later 6282319] edited the content of merge.txt to cause a conflict
1 file changed, 1 insertion(+), 1 deletion(-)

Executing the above command will produce the following effects:

  • Create and check new_ branch_ merge_ Branch of later
  • Overwrite merge Txt file
  • Submit new content

In this newly created branch new_ branch_ to_ merge_ In later, we submitted the rewritten merge Txt content

git checkout main
Switched to branch 'main'
echo "content to append" >> merge.txt
git commit -am"appended content to merge.txt"
[main 24fbe3c] appended content to merge.tx
1 file changed, 1 insertion(+)

The above series of commands first check out the main branch, and then send it to merge Txt file, and then submit. So far, the branch status in our warehouse is: two branches, main and new_branch_to_merge_later has two new submissions. Then let's try git merge new_ branch_ to_ merge_ What happens to the later command~

$ git merge new_branch_to_merge_later
Auto-merging merge.txt
CONFLICT (content): Merge conflict in merge.txt
Automatic merge failed; fix conflicts and then commit the result.

Bang Ji, sure enough, there was a conflict. Thanks, Git.

How to identify conflict content

As our example above demonstrates, Git will output some description information on the command line to let us know that a conflict has occurred. Next, we can execute the git status command to further view the details of the conflict.

$ git status
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
(use "git merge --abort" to abort the merge)

Unmerged paths:
(use "git add <file>..." to mark resolution)

both modified:   merge.txt

The output of the git status command shows the file paths that were not successfully merged due to the conflict. Sure enough, merge Txt file appears in the status bar where both sides of the merge modify. Next, you need to open the file to see what specific changes have occurred that cannot be merged.

$ cat merge.txt
<<<<<<< HEAD
this is some content to mess with
content to append
totally different content to merge later
>>>>>>> new_branch_to_merge_later

In the above command, we use the cat command to display merge Txt file. You can see some strange things

  • <<<<<<< HEAD
  • =======
  • >>>>>>> new_branch_to_merge_later

These can be seen as "dividing lines" of content====== The line of is in the middle of the conflict. The lines between the middle of the conflict and < < < < HEAD represent the content that exists in the current branch, that is, the branch pointed to by the HEAD pointer in the current warehouse. The corresponding content from the middle of the conflict to the > > > > > > > line is new_ branch_ to_ merge_ Content in the later branch.

How to use the command line to resolve conflicts

The most direct way to resolve conflicts is to modify the contents of conflicting files. Open merge with your favorite editor Txt file. In this case, we simply delete the meaningless conflict boundary. Then modify the merge Txt file looks like this:

this is some content to mess with
content to append
totally different content to merge later

Once the conflict file is modified, use git add merge Txt command to stage the new merged content. The way to finalize the final merger is also very simple. You can execute a commit through the following command:

git commit -m "merged and resolved the conflict in merge.txt"

At this time, Git will think that the conflict has been resolved, and then create a new merge submission to complete the whole merge.

Git commands that can be used to resolve merge conflicts

General tools

git status

The status command is a common command in the process of using Git, especially in the process of merge. It can help you identify which files are in conflict.

git log --merge

Passing the -- merge parameter to the git log command will list the specific submissions from the two branches that cause conflicts in the merge.

git diff

diff helps to find the similarities and differences between different states of a warehouse or file. This can often be used to predict or prevent mergers that may lead to conflicts.

Common tools in conflict scenarios when merging is started

git checkout

checkout can be used to undo changes to files or branches

git reset --mixed

reset is used to undo the current working directory or changes that have entered the staging area

Common tools in conflict scenarios during merging

git merge --abort

When the git merge command is executed, the -- abort option will exit the merge and return to the state of the current branch before the merge

git reset

git reset is used to reset a file to the version you know should be normal when a merge conflict occurs


Merger conflict can be a frightening experience. Fortunately, GIT provides powerful tools to help find and resolve conflicts. Git can handle most scenes for automatic merging. In git, the general conflict scenario is that two independent branches modify the same file, or when a file is deleted in one branch and another branch modifies the same file. Conflicts usually occur in a team environment.

There are many tools for resolving merge conflicts. In this article, we discussed many command-line tools provided by Git. In addition, there are many third-party tools to provide conflict resolution tools.

Keywords: git

Added by mikelmao on Mon, 28 Feb 2022 15:53:59 +0200