Git rebase vs merge for integrating changes from another branch or upstream

Git merge will require  to create new commit if there is a conflict. Git rebase will not create new commit. Git merge will show two lines combined in the merge commit (having two parents – from both branches) in history graph. Git rebase will show straight line with local commits on top of the other branch work.

mkdir git-rebase-vs-merge
cd git-rebase-vs-merge
git init
echo "master: created" > file.txt
git add file.txt
git commit -am "master: created"
git checkout -b feature-XX
echo "feature-XX: append 1x" >> file.txt
git commit -am "feature-XX: append 1x"
git checkout master
echo "master: append 1x" >> file.txt
git commit -am "master: append 1x"
git checkout feature-XX
echo "feature-XX: append 2x" >> file.txt
git commit -am "feature-XX: append 2x"
$ git log --decorate --abbrev-commit
commit bee2148 (HEAD -> feature-XX)
Date: Wed Feb 24 14:15:34 2016 +0100
 feature-XX: append 2x
commit 3314c4e
Date: Wed Feb 24 14:15:08 2016 +0100
 feature-XX: append 1x
commit 8829040
Date: Wed Feb 24 14:14:49 2016 +0100
 master: created
$ git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
* bee2148 - Wed, 24 Feb 2016 14:15:34 +0100 (7 minutes ago) (HEAD -> feature-XX)
| feature-XX: append 2x - Bartosz Wieczorek
* 3314c4e - Wed, 24 Feb 2016 14:15:08 +0100 (7 minutes ago)
| feature-XX: append 1x - Bartosz Wieczorek
| * 66aaede - Wed, 24 Feb 2016 14:15:23 +0100 (7 minutes ago) (master)
|/ master: append 1x - Bartosz Wieczorek
* 8829040 - Wed, 24 Feb 2016 14:14:49 +0100 (8 minutes ago)
 master: created - Bartosz Wieczorek$ cp -r ../git-rebase-vs-merge ../git-rebase-vs-merge-MERGE/
$ cp -r ../git-rebase-vs-merge ../git-rebase-vs-merge-REBASE/

 

$ cd ../git-rebase-vs-merge-MERGE/
$ git merge master
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat file.txt
master: created
<<<<<<< HEAD
feature-XX: append 1x
feature-XX: append 2x
||||||| merged common ancestors
=======
master: append 1x
>>>>>>> master
$ vim file.txt
$ cat file.txt
master: created
feature-XX: append 1x
feature-XX: append 2x
master: append 1x
$ git status
On branch feature-XX
You have unmerged paths.
 (fix conflicts and run "git commit")
Unmerged paths:
 (use "git add <file>..." to mark resolution)
 both modified: file.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ git add file.txt
$ git commit -am "feature-XX: merged changes from master to feature-XX branch"
[feature-XX 5373f86] feature-XX: merged changes from master to feature-XX branch
$ git log --decorate --abbrev-commit
commit 5373f86 (HEAD -> feature-XX)
Merge: bee2148 66aaede
Date: Wed Feb 24 14:19:03 2016 +0100
 feature-XX: merged changes from master to feature-XX branch
commit bee2148
Date: Wed Feb 24 14:15:34 2016 +0100
 feature-XX: append 2x
commit 66aaede (master)
Date: Wed Feb 24 14:15:23 2016 +0100
 master: append 1x
commit 3314c4e
Date: Wed Feb 24 14:15:08 2016 +0100
 feature-XX: append 1x
commit 8829040
Date: Wed Feb 24 14:14:49 2016 +0100
 master: created
$ git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
* 5373f86 - Wed, 24 Feb 2016 14:19:03 +0100 (10 seconds ago) (HEAD -> feature-XX)
|\ feature-XX: merged changes from master to feature-XX branch - Bartosz Wieczorek
| * 66aaede - Wed, 24 Feb 2016 14:15:23 +0100 (4 minutes ago) (master)
| | master: append 1x - Bartosz Wieczorek
* | bee2148 - Wed, 24 Feb 2016 14:15:34 +0100 (4 minutes ago)
| | feature-XX: append 2x - Bartosz Wieczorek
* | 3314c4e - Wed, 24 Feb 2016 14:15:08 +0100 (4 minutes ago)
|/ feature-XX: append 1x - Bartosz Wieczorek
* 8829040 - Wed, 24 Feb 2016 14:14:49 +0100 (4 minutes ago)
 master: created - Bartosz Wieczorek
$ cd ../git-rebase-vs-merge-REBASE/
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying: feature-XX: append 1x
Using index info to reconstruct a base tree...
M file.txt
Falling back to patching base and 3-way merge...
Auto-merging file.txt
CONFLICT (content): Merge conflict in file.txt
error: Failed to merge in the changes.
Patch failed at 0001 feature-XX: append 1x
The copy of the patch that failed is found in: .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.txt
master: created
<<<<<<< 66aaede288e55d4831c82ae3549f6f67e166b742
master: append 1x
||||||| merged common ancestors
=======
feature-XX: append 1x
>>>>>>> feature-XX: append 1x
$ vim file.txt
$ cat file.txt
master: created
master: append 1x
feature-XX: append 1x
$ git add file.txt
$ git rebase --continue
Applying: feature-XX: append 1x
Applying: feature-XX: append 2x
Using index info to reconstruct a base tree...
M file.txt
Falling back to patching base and 3-way merge...
Auto-merging file.txt
$ git status
On branch feature-XX
nothing to commit, working directory clean
$ git log --decorate --abbrev-commit
commit cbe6866 (HEAD -> feature-XX)
Date: Wed Feb 24 14:15:34 2016 +0100
 feature-XX: append 2x
commit 635f3d3
Date: Wed Feb 24 14:15:08 2016 +0100
 feature-XX: append 1x
commit 66aaede (master)
Date: Wed Feb 24 14:15:23 2016 +0100
 master: append 1x
commit 8829040
Date: Wed Feb 24 14:14:49 2016 +0100
 master: created
$ git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) %C(bold green)(%ar)%C(reset)%C(bold yellow)%d%C(reset)%n'' %C(white)%s%C(reset) %C(dim white)- %an%C(reset)' --all
* cbe6866 - Wed, 24 Feb 2016 14:15:34 +0100 (8 minutes ago) (HEAD -> feature-XX)
| feature-XX: append 2x - Bartosz Wieczorek
* 635f3d3 - Wed, 24 Feb 2016 14:15:08 +0100 (8 minutes ago)
| feature-XX: append 1x - Bartosz Wieczorek
* 66aaede - Wed, 24 Feb 2016 14:15:23 +0100 (8 minutes ago) (master)
| master: append 1x - Bartosz Wieczorek
* 8829040 - Wed, 24 Feb 2016 14:14:49 +0100 (8 minutes ago)
 master: created - Bartosz Wieczorek

 

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