This page is short tutorial about how to work with git conflicts.

Prerequisites

1. You should some unix/linux like operating system.

2. Git program installed.

Download and unpack sample file

1. Download file from http://download.evolonline.org/evol/git/gitconflicts.tar.gz

2. Open shell in directory with saved archive.

3. Upack gitconflicts.tar.gz by command:

tar xf gitconflicts.tar.gz

4. Open directory gitconflicts:

cd gitconflicts

Git conflict tutorial

After complete prerequisites steps you have open shell in directory gitconflicts.

We will emulate two users activity for get conflict in git

Sample directories:

remoterepo - remote repository for two users.

user1 - local repository for user 1.

user2 - local repository for user 2.

User 1 actions

1. Go to user1 directory. Run command:

cd user1

2. Add line to test file (file1.txt). Run command:

echo "second line from user1" >> file1.txt

3. Commit changes by command:

git commit -a -m "user1 changes"

You will get result:

[master 339b5a7] user1 changes
 1 file changed, 1 insertion(+)

4. Push changes to remote repository:

git push

Now user1 work complete.

User 2 actions

1. Go to user2 directory. Run command:

cd ../user2

2. Add line to test file (file1.txt). Run command:

echo "second line from user2" >> file1.txt

3. Commit changes by command:

git commit -a -m "user2 changes"

You will get result:

[master f2781ad] user2 changes
 1 file changed, 1 insertion(+)

4. Push changes to remote repository:

git push

Now you got error what remote repository changed:

To ../remoterepo/
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to '../remoterepo/'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

For fix this issue you should pull and rebase your changes on top of remote branch by command:

git pull --rebase

And here you got conflict:

remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From ../remoterepo
   f1f4cd9..a86672f  master     -> origin/master
First, rewinding head to replay your work on top of it...
Applying: user2 changes
Using index info to reconstruct a base tree...
M       file1.txt
Falling back to patching base and 3-way merge...
Auto-merging file1.txt
CONFLICT (content): Merge conflict in file1.txt
Failed to merge in the changes.
Patch failed at 0001 user2 changes
The copy of the patch that failed is found in:
   ..../user2/.git/rebase-apply/patch

When you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".

Resolving conflict by user2

Previous error show what you have some kind of conflict in file file1.txt

Lets get more info about conflict why and where it happend?

1. Run command for get detail info about files:

git status

and you will see:

rebase in progress; onto a86672f
You are currently rebasing branch 'master' on 'a86672f'.
  (fix conflicts and then run "git rebase --continue")
  (use "git rebase --skip" to skip this patch)
  (use "git rebase --abort" to check out the original branch)

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

        both modified:   file1.txt

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

It show “both modified: file1.txt”. This mean this file was changed in remote and local repository at same time.

And need somehow resolve this.

2. Now run command for get more details what in files conflicting:

git diff

You will see:

diff --cc file1.txt
index f526862,d4610e8..0000000
--- a/file1.txt
+++ b/file1.txt
@@@ -1,2 -1,2 +1,6 @@@
  this is first line
++<<<<<<< a86672fd8d6f3ed4d941c0417826c684235a89a0
 +second line from user1
++=======
+ second line from user2
++>>>>>>> user2 changes

You see here some conflicts inside file1.txt. Conflicting lines “second line from user1” and “second line from user2”

3. Now open in directory user2 file file1.txt with any text editor and you will see something like this:

this is first line
<<<<<<< a86672fd8d6f3ed4d941c0417826c684235a89a0
second line from user1
=======
second line from user2
>>>>>>> user2 changes

In this file you can see diff inserted into file. And you should select one of two versions or even mixed version.

Here “«««< a86672fd8d6f3ed4d941c0417826c684235a89a0” mean start of diff block. Id here is id of remote commit.

Here “=======” mean separator between two versions of data.

And “»»»> user2 changes</code>” mean end of diff block. Inside it show name of your local commit.

Considering what we need both lines from user1 and user2.

1. Change file1.txt to format like this:

this is first line
second line from user1
second line from user2

2. Save file in text editor.

3. Because we changed conflicting file it must be readded to git.

Run command:

git add file1.txt

Lets check what we have in repository.

1. Run command for see diff:

git diff

You should see nothing. This mean no changes present in repository.

2. Run command see git status:

git status

And you will see:

rebase in progress; onto a86672f
You are currently rebasing branch 'master' on 'a86672f'.
  (all conflicts fixed: run "git rebase --continue")

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        modified:   file1.txt

Remember what before it was “both modified”?

Now we can try to continue rebase to other commits if they present. Run command:

git rebase --continue

You will see message what commit with name “user2 changes” applied.

Ok lets see again repository diff and status.

1. Check diff by command:

git diff

You will see nothing. This mean no changes present.

2. Check status by command:

git status

You will see:

On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
nothing to commit, working directory clean

“Your branch is ahead of ‘origin/master’ by 1 commit” mean you have one not pushed commit.

“nothing to commit, working directory clean” mean you not have any uncommited changes.

Now you can see commits log by command:

git log

And see commit from user1 and commit from user2.

And finally you can push it to remote repository by command:

git push

You will see:

Counting objects: 3, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 938 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To ../remoterepo/
   a86672f..de6d375  master -> master

Congratulation, you fixed git conflict. Is it really hard?

Navigation
Print/export
Languages