Git merge master vs rebase master

Scenario:
1. create file on master with content “Hi :”
2. create my-branch, update file to “Hi: m-b” and commit in my-branch
3. switch to master, modify file to “Hi: m: and commit in master
4. switch to my-branch, update file to “Hi m-bx2” and commit in my-branch
5. in my-branch either:
a) git rebase master
or
b) git merge master

Common scenario for steps 1-4:

$ echo "Hi:" > file
$ git add file 
$ git commit -am "m: created file"
[master (root-commit) 6028a7a] m: created file
 1 file changed, 1 insertion(+)
 create mode 100644 file
$ git checkout -b my-branch
Switched to a new branch 'my-branch'
$ vim file 
$ cat file 
Hi: m-b
$ git commit -am "m-b: added m-b"
[my-branch 25c716a] m-b: added m-b
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout master
Switched to branch 'master'
Your branch is based on 'origin/master', but the upstream is gone.
 (use "git branch --unset-upstream" to fixup)
$ vim file 
$ cat file 
Hi: m
$ git commit -am "m: added m"
[master 7e402ee] m: added m
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout my-branch 
Switched to branch 'my-branch'
$ vim file 
$ git commit -am "m-b: added x2"
[my-branch 7ad292e] m-b: added x2
 1 file changed, 1 insertion(+), 1 deletion(-)

REBASE CASE (up to as many conflict resolution needed for git merge as numer number commits in my-branch)

$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: m-b: added m-b
Using index info to reconstruct a base tree...
M file
Falling back to patching base and 3-way merge...
Auto-merging file
CONFLICT (content): Merge conflict in file
Failed to merge in the changes.
Patch failed at 0001 m-b: added m-b
The copy of the patch that failed is found in:
 /home/bartek/git/merging-conflicts/.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".
$ cat file 
<<<<<<< HEAD
Hi: m
=======
Hi: m-b
>>>>>>> m-b: added m-b
$ vim file 
$ cat file
Hi: m m-b
$ git add file 
$ git rebase --continue 
Applying: m-b: added m-b
Applying: m-b: added x2
Using index info to reconstruct a base tree...
M file
Falling back to patching base and 3-way merge...
Auto-merging file
CONFLICT (content): Merge conflict in file
Failed to merge in the changes.
Patch failed at 0002 m-b: added x2
The copy of the patch that failed is found in:
 /home/bartek/git/merging-conflicts/.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".
$ cat file 
<<<<<<< HEAD
Hi: m m-b 
=======
Hi: m-bx2
>>>>>>> m-b: added x2
$ vim file 
$ cat file 
Hi: m m-b2x 
$ git add file 
$ git rebase --continue 
Applying: m-b: added x2

MERGE CASE (only up to one conflict resolution needed for git merge regardless of number commits in my-branch): 

$ git merge master
Auto-merging file
CONFLICT (content): Merge conflict in file
Automatic merge failed; fix conflicts and then commit the result.
$ cat file 
<<<<<<< HEAD
Hi: m-bx2
=======
Hi: m
>>>>>>> master
$ vim file 
$ cat file 
Hi: m m-bx2
$ git add file 
$ git status
On branch my-branch
All conflicts fixed but you are still merging.
 (use "git commit" to conclude merge)
Changes to be committed:
 modified: file
$ git commit -m "m-b: resolved merge conflict with master changes"
[my-branch 80868a8] m-b: resolved merge conflict with master changes
ISSUE WITH PUSHING BRANCH THAT WAS REBASED (assuming it was pulled by someone else):
bartek@hp:~/git/rebase-first-then-merge$ git checkout my-branch 
bartek@hp:~/git/rebase-first-then-merge$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: my-branch: create file branch1
Applying: my-branch: appended file1
Applying: my-branch: initial create file branch2
Applying: my-branch: initial create file branch3
bartek@hp:~/git/rebase-first-then-merge$ git log
Date: Thu Jan 28 19:57:39 2016 +0100
 my-branch: initial create file branch3
Date: Thu Jan 28 19:51:16 2016 +0100
 my-branch: initial create file branch2
Date: Thu Jan 28 19:40:02 2016 +0100
 my-branch: appended file1
Date: Thu Jan 28 19:36:48 2016 +0100
 my-branch: create file branch1
Date: Thu Jan 28 19:56:56 2016 +0100
 master: initial create file4
Date: Thu Jan 28 19:52:21 2016 +0100
 master: initial create file3
...
Date: Thu Jan 28 19:35:16 2016 +0100
 master: create file1
bartek@hp:~/git/rebase-first-then-merge$ git push
Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 309 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://github.com/bmwieczorek/rebase-first-then-merge.git
 c9f7261..ab2bbfe master -> master
 ! [rejected] my-branch -> my-branch (non-fast-forward)
error: failed to push some refs to 'https://github.com/bmwieczorek/rebase-first-then-merge.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
bartek@hp:~/git/rebase-first-then-merge$ 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s