Skip to content

merge conflicts

Merge conflicts occur when multiple authors edit the same content or when one developer deletes a file while another developer was making changes on it. To solve this problem developers work in isolated branches. The git merge command is responsible for combining isolated branches and resolving conflicting edits.

Git makes the merging process easier than most version control systems. Git can automatically integrate new changes in many cases. But in the cases of conflicts, Git cannot automatically figure out what version is correct. It marks the file as being conflicted and stops the merging process.

mergeconflicts

Common merge interruptions

Errors can occur at two points: at the start of the merge process or during it. Note that these are typically caused by uncommitted local changes rather than actual content conflicts.

Merge failure on start

Git fails to start a merge because the pending changes in the working directory or staging area of the project would be overwritten by the commits being merged in. This happens because of pending local changes. To take control over the local state, the git stash, git checkout, git commit and git reset commands are used. The following message will show up when an error occurs on start:

git conflicts

bash
error: Your local changes to the following files would be overwritten by merge:

Failure during merge

The failure during merge tells that there is a conflict between the current local branch and the branch that is being merged in. The following message will show up when an error occurs during merge:

failure during merge

bash
CONFLICT (content): Merge conflict in example.txt
Automatic merge failed; fix conflicts and then commit the result.

Creating a merge conflict

Here, we will show you a simulation of how merge conflicts appear.

merge conflicts

bash
mkdir test-dir
cd test-dir
git init .
echo "some content" > example.txt
git add example.txt
git commit -m "initial commit"
[master (root-commit) a45c22d] initial commit
1 file changed, 1 insertion(+)
create mode 100644 example.txt

In the given example, we create a new directory named test-dir. Next, we create example.txt text file with some content and add it to the repository and commit it. As a result, we have a new repository with one master branch and example.txt file. The next step is creating another branch to use as a conflicting merge.

resolving conflicts

bash
git checkout -b branch_to_merge
echo "completely different content to merge later" > example.txt
git commit -m "edit the content of example.txt to make a conflict"
[branch_to_merge 4221135] edit the content of example.txt to make a conflict
1 file changed, 1 insertion(+), 1 deletion(-)

In the above example, we create and check out branch_to_merge branch. After creating it, we overwrite the content in example.txt and commit the change. After doing all this, the commit overrides the content of example.txt:

git merging conflicts

bash
git checkout master
Switched to branch 'master'
echo "content to add" >> example.txt
git commit -m "added content to example.txt"
[master 11ab34b] added content to example.txt
1 file changed, 1 insertion(+)

These commands switch to the master branch, append content to example.txt, and commit the change. This puts the repository in a state where the master branch and branch_to_merge branch each have a unique commit. The final step is to execute the git merge command after which conflict will occur:

git merge

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

Identifying merge conflicts

As we have already seen, Git displays output which indicates that a conflict has appeared. Execute the git status command to see the unmerged paths:

git conflicts

bash
git status
On branch master
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:   example.txt

The example.txt file appears in a modified state. Execute the cat command to display the contents of the example.txt file. We can see these visual markers:

git resolving conflicts

bash
<<<<<<< HEAD
=======
>>>>>>> branch_to_merge

The ======= marker indicates the center of the conflict. The content between the <<<<<<< HEAD and ======= markers belongs to the current branch (master) that the HEAD reference points to. To cancel the merge entirely and return to the pre-merge state, run git merge --abort. Read more about visual markers on the git merge page.

Resolving merge conflicts

To resolve a merge conflict you should edit the conflicted file. Open the example.txt file in an editor, remove the conflict markers, and keep the desired changes. A resolved file might look like this:

conflicts in git

bash
some content to mess with
content to add

Execute the git add command to stage the new merge content. Next, create a new commit to complete the merge:

resolving conflicts in git

bash
git add example.txt
git commit -m "the conflict in example.txt is merged and resolved"

Alternatively, you can use git mergetool to resolve conflicts visually using a configured diff tool. For example, run git mergetool example.txt to open the tool for a specific file.

ToolDescription
git statusHelps find out conflicted files.
git mergetoolOpens a visual diff tool to resolve conflicts interactively.
git diffShows differences between commits, branches, or files to help identify potential conflicts before merging.
git checkout --ours/--theirsReplaces the conflicted file with content from the current or incoming branch.
git reset --mixedUnstages files but leaves the working directory unchanged.
git merge --abortAborts the current merge and restores the working directory to the state before the merge started.
git resetResets the index to match HEAD, helping to unstage conflicted files.

Practice

What are the aspects of handling merge conflicts in Git?

Dual-run preview — compare with live Symfony routes.