JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Resolving merge conflicts in Git

Author:JIYIK Last Updated:2025/03/31 Views:

In this article, we will demonstrate how to resolve conflicts that arise when merging two branches in Git.

Typically, different team members in a collaborative team environment work on the same files. For example, a team member working on some feature might edit READMEa file to provide information about the feature being developed.

Another team member working on a bug fix might READMEadd information about the fixed bug in the same file. They might work on different branches and commit their changes in their respective branches.

Sometimes, different branches need to be merged to provide a cohesive build. Therefore, this operation can lead to merge conflicts when the same files are updated in different branches.

Then the conflicts need to be resolved and the changes merged in. We will now illustrate this with an example.


Use git mergetoolto resolve conflicts when merging two branches

Before resolving merge conflicts, we should set up the diff tool used by Git as follows.

$ git config merge.tool meld
$ git config merge.conflictstyle diff3
$ git config mergetool.prompt false

The command above meldsets as the default diff tool. Additionally, we have conflictstyleset to diff3; this sets the diff tool to show the common ancestor of the two files (the current branch one and the branch to be merged in the branch).

To view the different diff tools supported, run the following command.

$ git mergetool --tool-help

Now, let's start with an example to demonstrate how to resolve conflicts. Suppose we have two branches as shown below.

$ git branch
* main
  feature1

The first branch is mainthe branch, and the second is feature1a feature development branch named .

We mainhave a README.mdfile in the branch with the following content.

$ cat README.md
# Upwork
Upwork projects

As shown above, we are currently in mainthe branch. Then, we switch to feature1the branch.

$ git checkout feature1
Switched to branch 'feature1'

$ git branch
  main
* feature1

We update README.mdand print its content as follows.

$ cat README.md
This is conflicting branch line.

Now, we merge mainthe branch with feature1the branch to get the latest changes in that branch.

While merging, Git shows merge conflicts as follows.

$ git merge main
Auto-merging README.md
CONFLICT (add/add): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.

We can get more information about the conflict in the following ways.

$ git status
On branch feature1
You have unmerged paths.
  (fix conflicts and run "git commit")

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

	both added:      README.md

no changes added to commit (use "git add" and/or "git commit -a")

We will now print README.mdthe contents of the file , which has the conflict.

$ cat README.md 
<<<<<<< HEAD
This is conflicting branch line.

||||||| merged common ancestors
=======
# Upwork
Upwork projects
>>>>>>> main

The file now displays various symbols.

The symbol <<<<<<<followed by HEADis an alias for the current branch. This indicates the start of editing in this section.

========The symbol indicates the end of a revision in the current branch and the beginning of edits in the new branch.

The symbol >>>>>>>>is followed by the remote branch name, i.e. main, showing where the merge was attempted.

Now, we will use mergetoolto resolve the conflict.

$ git mergetool
Merging:
README.md

Normal merge conflict for 'README.md':
  {local}: created file
  {remote}: created file

This will launch meld(since we have set it as the default diff tool). See the image below.

git-mergetool-meld1

The left pane shows the edits made to the file in the local branch README.md. The middle pane contains the results of the changes made to resolve the conflict. The right pane shows the edits made in the remote branch (i.e. the branch we want to merge into) main.

We can choose to keep both local and remote changes or just one of them. We will choose to keep remote branch changes as follows.

git-mergetool-meld2

We will then save and exit meldthe tool.

We will print README.mdthe document and check for updates.

$ cat README.md
# Upwork
Upwork projects

We will now commit the changes to Git.

$ git commit -m "merged from main"
[feature1 3c39d7b] merged from main

We will now run git diffthe command to check for conflicts between the feature1and branches.main

$ git diff feature1 main

We will check feature1the status of the branch.

$ git status
On branch feature1

Untracked files:
  (use "git add <file>..." to include in what will be committed)

	README.md.orig

It shows a file that was created README.md.origby when merging .mergetool

Run the following command to remove it:

$ git clean -f

So, we have successfully resolved mergetoolthe conflict when merging two branches using Git.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Git installation and establishment of local warehouse service

Publish Date:2025/04/05 Views:89 Category:Git

Git is a distributed version control system: the client does not only extract the latest version of the file snapshot, but also completely mirrors the original code repository. It has the following advantages: a. Since every extraction oper

git remote operation——multiple remote repositories for one project

Publish Date:2025/04/05 Views:131 Category:Git

Multiple remote repositories for a git project In our git project, the command to operate the remote repository information is $ git remote # 查看当前所有的远程仓库的名称 $ git remote -v # 查看远程仓库的名称和远程仓

Git cherry pick command usage

Publish Date:2025/04/05 Views:190 Category:Git

git cherry-pick is a powerful command that allows us to select an arbitrary Git commit by reference and attach it to the HEAD of the current working branch. Cherry picking is the act of picking a commit from one branch and applying it to an

Comparison between Git merge and Git rebase

Publish Date:2025/04/05 Views:171 Category:Git

The git rebase command may seem like Git wizardry to beginners, but if used carefully, it can actually make life easier for your development team. In this article, we compare git rebase with the related git merge command and identify all th

How to fix Git error Error: src refspec master does not match any

Publish Date:2025/04/05 Views:124 Category:Git

When using Git, we may encounter the error "src refspace master does not match any". Here's what the error means and how to fix it. What does src refspec master does not match any Mean in Git mean? We may encounter this error when we try to

Undo Git Stash

Publish Date:2025/04/04 Views:187 Category:Git

This article explains how to make and save changes to a repository. Git allows you to save changes locally and push them to a server when needed. In Git, we don't use the term save , but commit . We use git add , git commit , and git stash

View a list of cache entries in Git

Publish Date:2025/04/04 Views:59 Category:Git

We often need to pause our work and focus on something else in our development environment. Therefore, we may need to temporarily save our current work and focus on a different one. We may want to resume our original work later. git stash T

Git stores specific files

Publish Date:2025/04/04 Views:115 Category:Git

This article will cover storing changes to only specific files in Git. In Git, when we make some changes in our working tree, we may have some changes which may or may not be staged in our local repo. We may now wish to save these changes f

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial