You are on page 1of 14

Basic Merge Conflicts

by Caleb Espinoza G.
Merge Conflicts
● Conflicts generally arise when two people have changed
the same lines in a file.
● Conflicts only affect the developer conducting the
merge, the rest of the team is unaware of the conflict.
● For example: one developer deleted a file while another
developer was modifying it.
How Git handles the conflicts.
● Git will figure out how to automatically integrate new
changes.
● Git cannot automatically determine what is correct.
● Git will mark the file as being conflicted and halt the
merging process.
● It is the developers' responsibility to resolve the
conflict.
Let's create a merge conflict
1. Initialize a repo and commit a change:
● $ mkdir git-merge-test
● $ cd git-merge-test
● $ git init .
● $ echo "Please, don't touch this line" > merge.txt
● $ git add merge.txt
● $ git commit -m "we are committing the initial content"
Let's create a merge conflict
2. Create a new branch and edit the file at the same line:
● $ 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"
With this new branch: new_branch_to_merge_later we have
created a commit that overrides the content of merge.txt
Let's create a merge conflict
3. Add content to merge.txt
● $ git checkout master
● $ echo "content to add" >> merge.txt
● $ git commit -am "added content to merge.txt"
Now, we have 2 commits, one in the master branch and one in
the new_branch_to_merge_later branch.
Let's create a merge conflict
4. Lets git merge new_branch_to_merge_later
● $ 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.

BOOM 💥
A conflict appears!
How to identify which files have merge conflicts
Check the status
● $ 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: merge.txt
How to SOLVE merge conflicts
Open the merge.txt file in your favorite editor.
● $ cat merge.txt
● <<<<<<< HEAD
● Please, don't touch this line
● content to append
● =======
● totally different content to merge later
● >>>>>>> new_branch_to_merge_later
How to SOLVE merge conflicts
● Think of these new lines as "conflict dividers".
● The ======= line is the "center" of the conflict.
● All the content between the center and the <<<<<<<
HEAD line is content that exists in the current branch
which the HEAD ref is pointing to.
● Alternatively all content between the center and
>>>>>>> new_branch_to_merge_later is content that is
present in our merging branch.
How to SOLVE merge conflicts
Lets simply remove all the conflict dividers.
● Please, don't touch this line
● content to append
● totally different content to merge later
Now, lets run the following commands:
● $ git add merge.txt
● $ git commit -m "merged and solved the conflict in
merge.txt"
General tools
Lets simply remove all the conflict dividers.
● git status
● git log --merge
● git diff
● git merge --abort
● git reset
Thank you so much!

You might also like