Attention: Confluence is not suitable for the storage of highly confidential data. Please ensure that any data classified as Highly Protected is stored using a more secure platform.
If you have any questions, please refer to the University's data classification guide or contact ict.askcyber@sydney.edu.au

4. Dealing with conflicts

Sometimes we will have to deal with situations where the same file was changed in different branches, usually in the same line, and, therefore, GIT is unable to automatically merge changes

1. Creating our README BRANCH version of the feature

$ git checkout readme
Switched to branch 'readme'

$ echo "This is my conflicting line created in the README branch" >> README.txt

$ cat README.txt
This is my readme file
This is our README feature being created on a different branch
This is my conflicting line created in the README branch

$ git commit -a -m "Adding conflicting line to README.txt"
[readme ccaae8d] Adding conflicting line to README.txt
 1 file changed, 1 insertion(+)

2. Creating our MASTER BRANCH version of the feature

$ git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'.

$ echo "This is my conflicting line created in the MASTER branch" >> README.txt

$ cat README.txt
This is my readme file
This is our README feature being created on a different branch
This is my conflicting line created in the MASTER branch

$ git commit -a -m "Adding conflicting line to README.txt"
[master 6b6ab9c] Adding conflicting line to README.txt
 1 file changed, 1 insertion(+)

3. What happens now if we try to merge README back into MASTER?

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

GIT detected a MERGE CONFLICT since we've changed the same line in both branches, it does not know which line to keep 

$ cat README.txt
This is my readme file
This is our README feature being created on a different branch
<<<<<<< HEAD
This is my conflicting line created in the MASTER branch
=======
This is my conflicting line created in the README branch
>>>>>>> readme

  • The file now has both lines and we need to manually change the file and remove the '>>>> HEAD' and '>>>> readme' tags so we can move on with our merging process

$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
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: README.txt

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

Git status shows that we need to fix conflicts before continuing with our merge/commit

We can now fix the conflict manually or use third party tool to do it, since we have a simple case lets do it manually

$ echo "This is my readme file" > README.txt
$ echo "This is our README feature being created on a different branch" >> README.txt
$ echo "This is my conflicting line created by both the MASTER and README branch" >> README.txt

Checking if our file is looking the way we expect

$ cat README.txt
This is my readme file
This is our README feature being created on a different branch
This is my conflicting line created by both the MASTER and README branch

Commiting our changes and fixing the conflicts

$ git commit -a -m "Fixing merge conflict"
[master 20e6512] Fixing merge conflict

Checking how our timeline looks like after the merge

$ git log
commit 20e651235a28fba9b18a02f87e503ff6006fdc80
Merge: 6b6ab9c ccaae8d
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 12:20:12 2017 +1000

    Fixing merge conflict

commit 6b6ab9c2de012fcd1010ccb0e2e13fb95bb1aea8
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 12:15:37 2017 +1000

    Adding conflicting line to README.txt

commit ccaae8df007a5aabcd6285e590b42d8c3c487ee3
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 12:15:19 2017 +1000

    Adding conflicting line to README.txt

commit 0a3a2509b7efe7a6f0e79ed3f9108850f0150cae
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 12:08:51 2017 +1000

    New Version of our README Feature

commit 8ac84931c3671d115cb8297612c7b7eb9b691653
Author: Rafael Possas <rafael.possas@sydney.edu.au>
Date: Fri May 26 11:22:05 2017 +1000

    My first commit & Understanding Diff & Add Readme.txt

The conflicts have been resolved and both branches were merged

  • Push updated repository to GitHub

$ git push origin master

Counting objects: 9, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (9/9), done.
Writing objects: 100% (9/9), 937 bytes | 0 bytes/s, done.
Total 9 (delta 3), reused 0 (delta 0)
remote: Resolving deltas: 100% (3/3), done.
To https://github.com/rafaelpossas/git_tutorial.git
   0a3a250..20e6512  master -> master