DEV Community


Posted on

Conflict Resolution: Git Merge practice

This assumes you have some basic knowledge of git and the command line (I use bash). For reference I’ve included a few of the commands used in the bottom with some definition.

I’m pretty new to git and the command line but I’d like to share a few things I’m learning. I've found that it is good to practice using git with a fake project right in the terminal. Just reading about git commands and what they do does not help experience using git itself. Admittedly it’s a bit intimidating to try some things on a real project. So, open up terminal and use this to practice merging a conflict.

We're going to go through the process of doing a merge with a conflict in git. We will see what happens and then what needs to be done to resolve it.

Get Started and Create a Conflict

First, set up a folder to use for this, change into that directory and then do the following:

git init
echo "line 1" > conflict.txt
git add .
git commit -m "line 1"
echo "line 2" >> conflict.txt
git commit -am "add line 2"

We are on the master branch and now have a file (conflict.txt) that has two lines of text.

Next, we are going to branch off and make more changes on this file:

git checkout -b morelines
echo "line 3" >> conflict.txt
git commit -am "add line 3"

We are on the morelines branch. Right now conflict.txt on each branch looks like this:

Line # conflict.txt (master) conflict.txt (morelines)
1 line 1 line 1
2 line 2 line 2
3   line 3

If we were to now run git checkout master followed by git merge morelines, git will be able to merge with no conflict and it would be a fast-forward merge.

BUT! We want conflict! So we're going to get in a fight, sort of.

Do this:

git checkout master
echo "line 4" >> conflict.txt
git commit -am "add line 4"

If you could look at both files side by side right now they'd be like this:

Line # conflict.txt (master) conflict.txt (morelines)
1 line 1 line 1
2 line 2 line 2
3 line 4 line 3

You can see the conflict here.

Try the Merge

Now let's try to merge the morelines branch into the master branch and see what happens. We are already on master so do this:

git merge morelines

In our attempt to merge, git gives us this feedback:

Auto-merging conflict.txt

CONFLICT (content): Merge conflict in conflict.txt

Automatic merge failed; fix conflicts and then commit the result.

We have a conflict! Also note- git is in a merge process right now and expects us to do a few things. If you want to stop the merge process you can run git merge --abort. But we aren’t going to do that. We are going to fix the conflict and finish the merge.

Fix The Conflicts

In the above message, git tells us what to do next: "Fix conflicts and then commit the result." In other words:

Step 1: Open the conflicted file and merge the changes with edits.

Step 2: Stage the change.

Step 3: Run git commit to finish the merge process (this will make a merge commit).

Before continuing, you may run git status and read what it says. I’m not going to write about it here but just give it a good read.

Next, let’s fix the conflict.

Step 1: Open the conflicted file and merge the changes with edits.

This means what it means. Nothing is automatic here. You have to look at the conflicted file with your eyeballs and manually make changes.

Run open conflict.txt to open the file. It will look like this:

line 1
line 2
<<<<<<< HEAD
line 4
line 3
>>>>>>> morelines

Well this certainly looks strange! Let’s break it down a little. We can easily see the text we put there ourselves. But git has added extra markers. They have meaning:

The HEAD section shows the conflicted part of the currently checked out version:

<<<<<<< HEAD
line 4

The above conflicts with the incoming change that is indicated by the morelines section:

line 3
>>>>>>> morelines

In summary: there’s an incoming change from the branch we want to merge (morelines) that conflicts with the version that we have checked out (HEAD).

Git includes all of this to help us manually merge the changes.

We will need to delete those markers and re-arrange the text a bit. For this to make sense visually, go ahead and edit the file so it looks like this in the end:

line 1
line 2
line 3
line 4

As you can see, the lines we want are in the correct order, the marker lines are not there, and the separator line is gone.

Side Note: if you do not delete the markers that git placed in the file they will stay there. Git doesn’t take them away.

Side note, again: You can technically make any change you want at this point, but that is not the purpose of merging. Stick to merging changes together, not to making more changes. Save that for normal commits.

Save and close the file.

Step 2 and 3: Stage and Commit

In terminal:

git add .
git commit 

Your editor will come up with the commit text and git has conveniently included the commit message as Merge branch 'morelines'. This is okay for our fake project so save it and close.

Side Note: you won’t be able to complete the merge until all conflicts are resolved. If need be, repeat steps 1 and 2 and in the end do the commit to finish the merge.

Merge complete! You can even check two things to be sure: git status to show that we are on master branch. To see our line 3 text from morelines branch is merged into conflict.txt you can run cat conflict.txt.

Wrapping Up

Resolving a merge conflict:

  1. git merge branchname
  2. git will tell you there is a conflict (if any)
  3. Open and edit the conflicted file and merge the changes. Delete the references and separator that git puts in.
  4. Save the file.
  5. Use git add to stage your edit.
  6. Use git commit to complete the merge process.
  7. Done.

There could be more to a merge conflict than this, especially when merging someone else’s work into your work or something similar. More communication may be necessary and etc. But I hope this has been helpful just to see the basics.

Reference for some Commands

Here’s a short definition for some of the commands we used. Git commands start with git:

Command What it does
echo "text" > filename write "text" to the file filename
echo "text" >> filename append "text" to the file filename (notice the two arrows >>)
git add . stage changes in current directory
git commit -am "message" shortcut to stage all changes in project (regardless of directory) and commit with a message. Equivalent of git add -A; git commit -m "message"
git checkout -b branchname shortcut to create a branch (if it doesn’t exist) then check it out. Equivalent of git branch branchname; git checkout branchname

Top comments (2)

thomasrayner profile image
Thomas Rayner

This is fantastic. Thanks for the write up.

cookrdan profile image

Thank you!