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 diff shows diff details of uncommitted changes

Publish Date:2025/03/31 Views:105 Category:Git

This article outlines how we can get diff details of uncommitted work in Git. We use the git diff command to show the differences between various Git references, such as commits, the index, and the working tree. We can use this command to d

Staging area in Git

Publish Date:2025/03/31 Views:151 Category:Git

In this article, we will learn about the staging area in Git . Git is a version control system that maintains a history of changes made to a project directory. Git uses commits to track changes. Git has three internal management systems, on

Add all files in a folder to commit in Git

Publish Date:2025/03/31 Views:158 Category:Git

This article will discuss the necessary steps to add all of your files into one folder for submission. If you have a folder with a dozen files, adding the files one by one can be tedious. Fortunately, Git allows us to add all the contents o

Meaning of Fetch_Head in Git

Publish Date:2025/03/31 Views:64 Category:Git

This article defines Fetch_HEAD in Git . This reference is an integral part of the git pull command and is important when merging changes from a remote repository into a local repository or branch. If you're not sure what Fetch_Head means,

Get all branches in Git

Publish Date:2025/03/31 Views:62 Category:Git

This article discusses how to fetch all branches from a remote repository. The git fetch command is a useful utility when you want to download changes from a remote repository without having to update your local branches. Sometimes, you may

Clone a Git repository with a specific revision

Publish Date:2025/03/31 Views:82 Category:Git

This article discussed various methods that we can use to clone a Git repository with a specific revision or changeset. This comes in handy when you have a repository with large files and you only need a specific version of the code. Instea

Squash commits pushed in Git

Publish Date:2025/03/31 Views:86 Category:Git

This article outlines the process of squashing commits that we have pushed to a remote repository. We squash the commits into one to reduce clutter in the repository. To squash the commits, we run git rebase in interactive mode . Squash com

Git squash all commits

Publish Date:2025/03/31 Views:65 Category:Git

In every developer’s life, the word squash is often used while working with the Git distributed control system . This feature in Git is a handy option that developers often use to achieve a neat workflow in their development team. In this

Close the Git commit editor on Windows

Publish Date:2025/03/31 Views:62 Category:Git

In this article, we will discuss how to exit the Git commit editor. This can be a little tricky, especially if you are new to Git bash . Let's see how to exit the editor on Windows. Close the Git commit editor on Windows We will look at a t

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial