Skip to content

Instantly share code, notes, and snippets.

@hughsaunders
Last active December 20, 2015 03:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hughsaunders/6062300 to your computer and use it in GitHub Desktop.
Save hughsaunders/6062300 to your computer and use it in GitHub Desktop.
merge conflict resolution

Simple example of a merge conflict

####Overview

  • add a file
  • create new branch
  • modify same section of file in both branches in a conflicting way
  • attempt to merge one branch into the other

####Process

  1. add file
(default27)MK63HADV33:testrepo hugh3869$ echo -e "a\nb\nc" >> mergetest
(default27)MK63HADV33:testrepo hugh3869$ cat mergetest
a
b
c
  1. commit
(default27)MK63HADV33:testrepo hugh3869$ git commit -a -m "added merge test"
[master 8d2f76a] added merge test
 1 files changed, 3 insertions(+), 0 deletions(-)
 create mode 100644 mergetest
  1. create feature branch but still on master (use checkout -b to branch and checkout)
(default27)MK63HADV33:testrepo hugh3869$ git branch feature
  1. make a change on master branch
(default27)MK63HADV33:testrepo hugh3869$ sed -i 's/b/d/' mergetest
  1. commit
(default27)MK63HADV33:testrepo hugh3869$ git commit -a -m "mergetest master change (b-->d)"
[master a393a81] mergetest master change (b-->d)
 1 files changed, 1 insertions(+), 1 deletions(-)
  1. switch to feature branch
(default27)MK63HADV33:testrepo hugh3869$ git checkout feature
Switched to branch 'feature'
  1. make a conflicting change
(default27)MK63HADV33:testrepo hugh3869$ sed -i 's/b/e/' mergetest
  1. commit
(default27)MK63HADV33:testrepo hugh3869$ git commit -a -m "mergetest feature change (b-->e)"
[feature 8682551] merge test feature change (b-->e)
 1 files changed, 1 insertions(+), 1 deletions(-)
  1. checkout master and attempt to merge
(default27)MK63HADV33:testrepo hugh3869$ git checkout master
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 2 commits.
(default27)MK63HADV33:testrepo hugh3869$ git merge feature
Auto-merging mergetest
CONFLICT (add/add): Merge conflict in mergetest
Automatic merge failed; fix conflicts and then commit the result.
  1. merge fails due to conflict, view the resulting file:
(default27)MK63HADV33:testrepo hugh3869$ cat mergetest
a
<<<<<<< HEAD
d
=======
e
>>>>>>> feature
c
  1. choose the option you want for the conflicting section (d/e), delete the other section and remove the delimiters (<<<===>>>)
(default27)MK63HADV33:testrepo hugh3869$ sed -i '/^[<=>e]/d' mergetest
  1. add and commit
(default27)MK63HADV33:testrepo hugh3869$ git add mergetest
(default27)MK63HADV33:testrepo hugh3869$ git commit -a -m "resolved feature merge"
  1. look at the history
(default27)MK63HADV33:testrepo hugh3869$ git log --graph --oneline
*   7954798 resolved feature merge
|\
| * 8682551 mergetest feature change (b-->e)
* | a393a81 mergetest master change (b-->d)
|/
* 8d2f76a added merge test
* 9e29f4d fix travis yaml
* 3c5de95 add .travis.yml
* e95a88b playing with travis
* 0b2fe45 Initial commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment