git pull merge vs rebase

Note: extract commands by calling:
"| grep '$ ' | sed 's/\$\ //'"
0. Initial status for all 2 cases below:
$ mkdir git-pull-merge-vs-rebase
$ cd git-pull-merge-vs-rebase
$ mkdir u1
$ cd u1
$ git clone https://github.com/bmwieczorek/git-pull-merge-vs-rebase.git
Cloning into 'git-pull-merge-vs-rebase'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
$ cd git-pull-merge-vs-rebase
$ echo "u1: initial create of file.txt" >> file.txt
$ git add file.txt
$ git commit -m "u1: created file.txt" file.txt
[master (root-commit) 54a0645] u1: created file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
$ git push origin master
Password for 'https://bmwieczorek@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 255 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://bmwieczorek@github.com/bmwieczorek/git-pull-merge-vs-rebase.git
 * [new branch] master -> master
$ echo "u1: append 1x file.txt" >> file.txt
$ git add file.txt
$ git commit -m "u1: append 1x file.txt"
[master 1d74c81] u1: append 1x file.txt
 1 file changed, 1 insertion(+)
$ # do not push yet

$ cd ../..
$ mkdir u2
$ cd u2
$ git clone https://github.com/bmwieczorek/git-pull-merge-vs-rebase.git
Cloning into 'git-pull-merge-vs-rebase'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
Checking connectivity... done.
$ cd git-pull-merge-vs-rebase
$ echo "u2: append 1x" >> file.txt
$ git add file.txt
$ git commit -m "u2: append 1x file.txt" file.txt
[master 5b0b88a] u2: append 1x file.txt
 1 file changed, 1 insertion(+)
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 304 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://bmwieczorek@github.com/bmwieczorek/git-pull-merge-vs-rebase.git
 54a0645..5b0b88a master -> master


$ cd ../../u1/git-pull-merge-vs-rebase
$ echo "u1: append 2x file.txt" >> file.txt
$ git add file.txt
$ git commit -m "u1: append 2x file.txt"
[master 9b4e921] u1: append 2x file.txt
 1 file changed, 1 insertion(+)
$ git push origin master
To https://bmwieczorek@github.com/bmwieczorek/git-pull-merge-vs-rebase.git
 ! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://bmwieczorek@github.com/bmwieczorek/git-pull-merge-vs-rebase.git'
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.
$ git log --graph --pretty=oneline --abbrev-commit --decorate --all
f4696de (HEAD -> master) u1: append 2x file.txt
281bc65 u1: append 1x file.txt
8665249 (origin/master) u1: created file.txt
1. git pull (fetch + merge)
$ git pull
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/bmwieczorek/git-pull-merge-vs-rebase
 54a0645..5b0b88a master -> origin/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
u1: initial create of file.txt
<<<<<<< HEAD
u1: append 1x file.txt
u1: append 2x file.txt
=======
u2: append 1x
>>>>>>> 5b0b88a264a20a81bb26a38ea63f03f8c35b8218

$ git log --graph --pretty=oneline --all --abbrev-commit --decorate
* 9b4e921 (HEAD -> master) u1: append 2x file.txt
* 1d74c81 u1: append 1x file.txt
| * 5b0b88a (origin/master) u2: append 1x file.txt
|/
* 54a0645 u1: created file.txt

$ git diff <tab>
FETCH_HEAD HEAD master MERGE_HEAD ORIG_HEAD origin/master

$ cat .git/FETCH_HEAD
5b0b88a264a20a81bb26a38ea63f03f8c35b8218 branch 'master' of https://github.com/bmwieczorek/git-pull-merge-vs-rebase

$ cat .git/MERGE_HEAD
5b0b88a264a20a81bb26a38ea63f03f8c35b8218

$ cat .git/ORIG_HEAD
9b4e9212aeb8245694c469324c11ab451bb59eff

$ cat file.txt | egrep -v '<<<|>>>|===' > file.txt # resolve conflict

$ cat file.txt
u1: initial create of file.txt
u1: append 1x file.txt
u1: append 2x file.txt
u2: append 1x

$ git status
On branch master
Your branch and 'origin/master' have diverged,
and have 2 and 1 different commit each, respectively.
 (use "git pull" to merge the remote branch into yours)
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 status
On branch master
Your branch and 'origin/master' have diverged,
and have 2 and 1 different commit each, respectively.
 (use "git pull" to merge the remote branch into yours)
All conflicts fixed but you are still merging.
 (use "git commit" to conclude merge)
Changes to be committed:
 modified: file.txt

$ git commit -m "u1: merged changes from master"
[master f286b34] u1: merged changes from master

$ git push origin master
Counting objects: 9, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (9/9), 795 bytes | 0 bytes/s, done.
Total 9 (delta 2), reused 0 (delta 0)
To https://bmwieczorek@github.com/bmwieczorek/git-pull-merge-vs-rebase.git
 5b0b88a..f286b34 master -> master
 
$ git diff <tab>
FETCH_HEAD HEAD master ORIG_HEAD origin/master

$ cat .git/FETCH_HEAD
5b0b88a264a20a81bb26a38ea63f03f8c35b8218 branch 'master' of https://github.com/bmwieczorek/git-pull-merge-vs-rebase

$ cat .git/ORIG_HEAD
9b4e9212aeb8245694c469324c11ab451bb59eff

$ git log --abbrev-commit --decorate # note that history is in the chronological order of commits (not pushes) + last merge commit
commit f286b34 (HEAD -> master, origin/master)
Merge: 9b4e921 5b0b88a
Date: Sun Feb 14 16:38:44 2016 +0100
 u1: merged changes from master
commit 9b4e921
Date: Sun Feb 14 14:31:40 2016 +0100
 u1: append 2x file.txt
commit 5b0b88a
Date: Sun Feb 14 14:31:34 2016 +0100
 u2: append 1x file.txt
commit 1d74c81
Date: Sun Feb 14 14:31:31 2016 +0100
 u1: append 1x file.txt
commit 54a0645
Date: Sun Feb 14 14:30:33 2016 +0100
 u1: created file.txt

$ git log --graph --pretty=oneline --abbrev-commit --decorate
* f286b34 (HEAD -> master) u1: merged changes from master
|\
| * 5b0b88a (origin/master) u2: append 1x file.txt
* | 9b4e921 u1: append 2x file.txt
* | 1d74c81 u1: append 1x file.txt
|/
* 54a0645 u1: created file.txt
2. git pull with rebase (fetch + rebase)
$ git pull --rebase
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
From https://github.com/bmwieczorek/git-pull-merge-vs-rebase
 8665249..ae30041 master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: u1: append 1x file.txt
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 u1: append 1x file.txt
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".

$ git status
rebase in progress; onto ae30041
You are currently rebasing branch 'master' on 'ae30041'.
 (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: file.txt
no changes added to commit (use "git add" and/or "git commit -a")

$ cat file.txt
u1: initial create of file.txt
<<<<<<< ae3004164ee6fbfa5daf9dc339f494c7b1486891
u2: append 1x
=======
u1: append 1x file.txt
>>>>>>> u1: append 1x file.txt

$ cat file.txt | egrep -v '<<<|>>>|===' > file.txt # resolve conflict

$ cat file.txt
u1: initial create of file.txt
u2: append 1x
u1: append 1x file.txt

$ git log --pretty=oneline --abbrev-commit --decorate --all
f4696de (master) u1: append 2x file.txt
ae30041 (HEAD, origin/master) u2: append 1x file.txt
281bc65 u1: append 1x file.txt
8665249 u1: created file.txt

$ 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
* f4696de - Sun, 14 Feb 2016 17:18:55 +0100 (7 minutes ago) (master)
| u1: append 2x file.txt - Bartosz Wieczorek
* 281bc65 - Sun, 14 Feb 2016 17:18:50 +0100 (7 minutes ago)
| u1: append 1x file.txt - Bartosz Wieczorek
| * ae30041 - Sun, 14 Feb 2016 17:18:52 +0100 (7 minutes ago) (HEAD, origin/master)
|/ u2: append 1x file.txt - Bartosz Wieczorek
* 8665249 - Sun, 14 Feb 2016 17:18:47 +0100 (7 minutes ago)
 u1: created file.txt - Bartosz Wieczorek

$ git diff
FETCH_HEAD HEAD master ORIG_HEAD origin/master

$ cat .git/FETCH_HEAD
ae3004164ee6fbfa5daf9dc339f494c7b1486891 branch 'master' of https://github.com/bmwieczorek/git-pull-merge-vs-rebase

$ cat .git/HEAD
ae3004164ee6fbfa5daf9dc339f494c7b1486891

$ cat .git/ORIG_HEAD
f4696de0cc8caa2a921cc0bc5ead80842097ba37

$ git status
rebase in progress; onto ae30041
You are currently rebasing branch 'master' on 'ae30041'.
 (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: file.txt
no changes added to commit (use "git add" and/or "git commit -a")

$ cat file.txt
u1: initial create of file.txt
u2: append 1x
u1: append 1x file.txt

$ git add file.txt

$ git rebase --continue
Applying: u1: append 1x file.txt
Applying: u1: append 2x file.txt
Using index info to reconstruct a base tree...
M file.txt
Falling back to patching base and 3-way merge...
Auto-merging file.txt

$ cat file.txt
u1: initial create of file.txt
u2: append 1x
u1: append 1x file.txt
u1: append 2x file.txt

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

$ 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
* 6326710 - Sun, 14 Feb 2016 17:18:55 +0100 (10 minutes ago) (HEAD -> master)
| u1: append 2x file.txt - Bartosz Wieczorek
* feb9d0c - Sun, 14 Feb 2016 17:18:50 +0100 (10 minutes ago)
| u1: append 1x file.txt - Bartosz Wieczorek
* ae30041 - Sun, 14 Feb 2016 17:18:52 +0100 (10 minutes ago) (origin/master)
| u2: append 1x file.txt - Bartosz Wieczorek
* 8665249 - Sun, 14 Feb 2016 17:18:47 +0100 (10 minutes ago)
 u1: created file.txt - Bartosz Wieczorek

$ git log --graph --pretty=oneline --abbrev-commit --decorate --all # note the order is first master branch changes then u1 two commits
* 6326710 (HEAD -> master) u1: append 2x file.txt
* feb9d0c u1: append 1x file.txt
* ae30041 (origin/master) u2: append 1x file.txt
* 8665249 u1: created file.txt

$ git push origin master
Counting objects: 6, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (6/6), 550 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To https://bmwieczorek@github.com/bmwieczorek/git-pull-merge-vs-rebase.git
 ae30041..6326710 master -> master

$ git log --graph --pretty=oneline --abbrev-commit --decorate --all
* 6326710 (HEAD -> master, origin/master) u1: append 2x file.txt
* feb9d0c u1: append 1x file.txt
* ae30041 u2: append 1x file.txt
* 8665249 u1: created file.txt




mkdir git-pull-merge-vs-rebase
cd git-pull-merge-vs-rebase
mkdir u1
cd u1
git clone https://github.com/bmwieczorek/git-pull-merge-vs-rebase.git
cd git-pull-merge-vs-rebase
echo "u1: initial create of file.txt" >> file.txt
git add file.txt
git commit -m "u1: created file.txt" file.txt
git push origin master
echo "u1: append 1x file.txt" >> file.txt
git add file.txt
git commit -m "u1: append 1x file.txt"
# do not push yet
cd ../..
mkdir u2
cd u2
git clone https://github.com/bmwieczorek/git-pull-merge-vs-rebase.git
cd git-pull-merge-vs-rebase
echo "u2: append 1x" >> file.txt
git add file.txt
git commit -m "u2: append 1x file.txt" file.txt
git push origin master
cd ../../u1/git-pull-merge-vs-rebase
echo "u1: append 2x file.txt" >> file.txt
git add file.txt
git commit -m "u1: append 2x file.txt"
git push origin master
git log --graph --pretty=oneline --abbrev-commit --decorate
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
git pull --rebase
git status
cat file.txt
cat file.txt | egrep -v '<<<|>>>|===' > file.txt # resolve conflict
cat file.txt
git log --graph --pretty=oneline --abbrev-commit --decorate
git log --graph --abbrev-commit --decorate --format=format:'%C(bold blue)%h%C(reset) - %C(bold cyan)%aD%C(reset) 
cat .git/FETCH_HEAD
cat .git/HEAD
cat .git/ORIG_HEAD
git status
cat file.txt
git add file.txt
git rebase --continue
cat file.txt
git status
git log --graph --pretty=oneline --abbrev-commit --decorate # note the order is first master branch changes then
 u1 two commits
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
git push origin master



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