git pull –rebase

Scenario:
U1 clone repo, create file, commit and push origin master
U1 first update file, commit (do not yet push)
U2 clone repo, update file, commit and push origin master
U1 second update file, commit, and try to push (rejected push)
U1 git pull --rebase, resolve conflict, git rebase --continue, git push origin master

History after rebase: local commits will be applied on the top of (after) commits that were already applied to origin master. In other words the local commits will be seen as last. Here, U2 commit will be reordered to be applied before 'U1 first update' commit even though the 'U1 first update' was chronologically first):
U1 second update file
U1 first update file
U2 update file
U1 commit (file create)
Commands:
mkdir git-pull-with-rebase
cd git-pull-with-rebase
mkdir u1
cd u1
git clone https://github.com/bmwieczorek/git-pull-with-rebase.git
cd git-pull-with-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-with-rebase.git
cd git-pull-with-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-with-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) %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
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
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
Results:
$ mkdir git-pull-with-rebase
$ cd git-pull-with-rebase
$ mkdir u1
$ cd u1
$ git clone https://github.com/bmwieczorek/git-pull-with-rebase.git
Cloning into 'git-pull-with-rebase'...
warning: You appear to have cloned an empty repository.
Checking connectivity... done.
$ cd git-pull-with-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) 743a0f8] u1: created file.txt
 1 file changed, 1 insertion(+)
 create mode 100644 file.txt
$ git push origin master
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-with-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 b4060fb] 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-with-rebase.git
Cloning into 'git-pull-with-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-with-rebase
$ echo "u2: append 1x" >> file.txt
$ git add file.txt
$ git commit -m "u2: append 1x file.txt" file.txt
[master e63fe7e] u2: append 1x file.txt
 1 file changed, 1 insertion(+)
$ git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 300 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://bmwieczorek@github.com/bmwieczorek/git-pull-with-rebase.git
 743a0f8..e63fe7e master -> master
$ cd ../../u1/git-pull-with-rebase
$ echo "u1: append 2x file.txt" >> file.txt
$ git add file.txt
$ git commit -m "u1: append 2x file.txt"
[master bc68886] u1: append 2x file.txt
 1 file changed, 1 insertion(+)
$ git push origin master
To https://bmwieczorek@github.com/bmwieczorek/git-pull-with-rebase.git
 ! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://bmwieczorek@github.com/bmwieczorek/git-pull-with-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
* bc68886 (HEAD -> master) u1: append 2x file.txt
* b4060fb u1: append 1x file.txt
* 743a0f8 (origin/master) 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
* bc68886 - Sun, 14 Feb 2016 22:35:40 +0100 (14 seconds ago) (HEAD -> master)
| u1: append 2x file.txt - Bartosz Wieczorek
* b4060fb - Sun, 14 Feb 2016 22:34:29 +0100 (85 seconds ago)
| u1: append 1x file.txt - Bartosz Wieczorek
* 743a0f8 - Sun, 14 Feb 2016 22:34:10 +0100 (2 minutes ago) (origin/master)
 u1: created file.txt - Bartosz Wieczorek
$ 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-with-rebase
 743a0f8..e63fe7e 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 e63fe7e
You are currently rebasing branch 'master' on 'e63fe7e'.
 (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
<<<<<<< e63fe7eaceb2b540544522e7121b79d7eb46337f
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 --graph --pretty=oneline --abbrev-commit --decorate
* e63fe7e (HEAD, origin/master) u2: append 1x file.txt
* 743a0f8 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
* bc68886 - Sun, 14 Feb 2016 22:35:40 +0100 (43 seconds ago) (master)
| u1: append 2x file.txt - Bartosz Wieczorek
* b4060fb - Sun, 14 Feb 2016 22:34:29 +0100 (2 minutes ago)
| u1: append 1x file.txt - Bartosz Wieczorek
| * e63fe7e - Sun, 14 Feb 2016 22:35:22 +0100 (61 seconds ago) (HEAD, origin/master)
|/ u2: append 1x file.txt - Bartosz Wieczorek
* 743a0f8 - Sun, 14 Feb 2016 22:34:10 +0100 (2 minutes ago)
 u1: created file.txt - Bartosz Wieczorek
$ cat .git/FETCH_HEAD
e63fe7eaceb2b540544522e7121b79d7eb46337f branch 'master' of https://github.com/bmwieczorek/git-pull-with-rebase
$ cat .git/HEAD
e63fe7eaceb2b540544522e7121b79d7eb46337f
$ cat .git/ORIG_HEAD
bc68886e2728e2382970bd98f01ff3d00ff8c5a4
$ git status
rebase in progress; onto e63fe7e
You are currently rebasing branch 'master' on 'e63fe7e'.
 (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")
$ 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 --pretty=oneline --abbrev-commit --decorate # origin/master U2 commit is applied before both U1 commits
* 5505349 (HEAD -> master) u1: append 2x file.txt
* f117b95 u1: append 1x file.txt
* e63fe7e (origin/master) u2: append 1x file.txt
* 743a0f8 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
* 5505349 - Sun, 14 Feb 2016 22:35:40 +0100 (2 minutes ago) (HEAD -> master)
| u1: append 2x file.txt - Bartosz Wieczorek
* f117b95 - Sun, 14 Feb 2016 22:34:29 +0100 (3 minutes ago)
| u1: append 1x file.txt - Bartosz Wieczorek
* e63fe7e - Sun, 14 Feb 2016 22:35:22 +0100 (2 minutes ago) (origin/master)
| u2: append 1x file.txt - Bartosz Wieczorek
* 743a0f8 - Sun, 14 Feb 2016 22:34:10 +0100 (3 minutes ago)
 u1: created file.txt - Bartosz Wieczorek
$ 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), 551 bytes | 0 bytes/s, done.
Total 6 (delta 1), reused 0 (delta 0)
To https://bmwieczorek@github.com/bmwieczorek/git-pull-with-rebase.git
 e63fe7e..5505349 master -> master
$ git log --graph --pretty=oneline --abbrev-commit --decorate
* 5505349 (HEAD -> master, origin/master) u1: append 2x file.txt
* f117b95 u1: append 1x file.txt
* e63fe7e u2: append 1x file.txt
* 743a0f8 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
* 5505349 - Sun, 14 Feb 2016 22:35:40 +0100 (2 minutes ago) (HEAD -> master, origin/master)
| u1: append 2x file.txt - Bartosz Wieczorek
* f117b95 - Sun, 14 Feb 2016 22:34:29 +0100 (4 minutes ago)
| u1: append 1x file.txt - Bartosz Wieczorek
* e63fe7e - Sun, 14 Feb 2016 22:35:22 +0100 (3 minutes ago)
| u2: append 1x file.txt - Bartosz Wieczorek
* 743a0f8 - Sun, 14 Feb 2016 22:34:10 +0100 (4 minutes ago)
 u1: created file.txt - 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