git pull –rebase scenarios

Scenario 1  (both users U1 and U2 are in sync; U2 modified committed and pushed file B; U1 executes git pull –rebase –> fast forward update file B):

  • U1: create file A and B commits it and pushes to repo
  • U2: pull changes
  • U2: modifies only B, commits and pushes
  • U1: pull –rebase #no conflicts, automatically U2 fast-forward commit in incorporated for U1 (no merge commit when rebasing)
#u1
git clone https://github.com/bmwieczorek/git-pull-rebase.git git-pull-rebase-u1
cd git-pull-rebase-u1
echo "u1 created a.txt" > a.txt
git add a.txt
git commit -m "u1 created a.txt"
echo "u1 created b.txt" > b.txt
git add b.txt
git commit -m "u1 created b.txt"
git push origin master
cd ..
#u2
git clone https://github.com/bmwieczorek/git-pull-rebase.git git-pull-rebase-u2
cd git-pull-rebase-u2
echo "u2 append 1x b.txt" >> b.txt
git add b.txt
git commit -am "u2 append 1x b.txt"
git push origin master
cd ..

#u1
cd git-pull-rebase-u1
git pull --rebase
$ git pull --rebase
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), 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-rebase
 625807c..23b1c44 master -> origin/master
First, rewinding head to replay your work on top of it...
Fast-forwarded master to 23b1c44a2f52387c46b24a1bfb144be34b1abc0a.
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
$ git log --decorate --abbrev-commit --stat | grep -v Author| egrep -v '^$'
commit 23b1c44 (HEAD -> master, origin/master)
Date: Fri Feb 26 09:46:10 2016 +0100
 u2 append 1x b.txt
 b.txt | 1 +
 1 file changed, 1 insertion(+)
commit 625807c
Date: Fri Feb 26 09:45:37 2016 +0100
 u1 created b.txt
 b.txt | 1 +
 1 file changed, 1 insertion(+)
commit e94be7f
Date: Fri Feb 26 09:45:37 2016 +0100
 u1 created a.txt
 a.txt | 1 +
 1 file changed, 1 insertion(+)
$ 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
* 23b1c44 - Fri, 26 Feb 2016 09:46:10 +0100 (8 minutes ago) (HEAD -> master, origin/master)
| u2 append 1x b.txt - Bartosz Wieczorek
* 625807c - Fri, 26 Feb 2016 09:45:37 +0100 (8 minutes ago)
| u1 created b.txt - Bartosz Wieczorek
* e94be7f - Fri, 26 Feb 2016 09:45:37 +0100 (8 minutes ago)
 u1 created a.txt - Bartosz Wieczorek

Scenario 2  (both users U1 and U2 are in sync; U1 modified and committed file A; U2 modified committed and pushed file B; U1 executes git pull –> automatic merge happens for file B (no conflict) and automatic merge commit is generated):

  • U1: create file A and B commits it and pushes to repo
  • U1: modifies A, commits but do not yet push
  • U2: pull changes
  • U2: modifies B, commits and pushes
  • U1: pull  –rebase #no conflicts, reset to common commit, applied U2 change then applied U1 (no merge commit for rebase)
#u1
git clone https://github.com/bmwieczorek/git-pull-rebase.git git-pull-rebase-u1
cd git-pull-rebase-u1
echo "u1 created a.txt" > a.txt
git add a.txt
git commit -m "u1 created a.txt" 
echo "u1 created b.txt" > b.txt
git add b.txt
git commit -m "u1 created b.txt"
git push origin master
echo "u1 append 1x a.txt" >> a.txt
git add a.txt
git commit -m "u1 appended a.txt" a.txt
cd ..
#u2
git clone https://github.com/bmwieczorek/git-pull-rebase.git git-pull-rebase-u2
cd git-pull-rebase-u2
echo "u2 append 1x b.txt" >> b.txt
git add b.txt
git commit -am "u2 append 1x b.txt"
git push origin master
cd ..
#u1
cd git-pull-rebase-u1
git pull --rebase
$ git pull --rebase
remote: Counting objects: 3, done.
remote: Compressing objects: 100% (2/2), 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-rebase
 d0af369..7ced2db master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: u1 appended a.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
 (use "git push" to publish your local commits)
nothing to commit, working directory clean
$ git log --decorate --abbrev-commit --stat | grep -v Author | sed -e /^$/d
commit e684266 (HEAD -> master)
Date: Fri Feb 26 10:13:48 2016 +0100
 u1 appended a.txt
 a.txt | 1 +
 1 file changed, 1 insertion(+)
commit 7ced2db (origin/master)
Date: Fri Feb 26 10:13:49 2016 +0100
 u2 append 1x b.txt
 b.txt | 1 +
 1 file changed, 1 insertion(+)
commit d0af369
Date: Fri Feb 26 10:13:45 2016 +0100
 u1 created b.txt
 b.txt | 1 +
 1 file changed, 1 insertion(+)
commit fb6668e
Date: Fri Feb 26 10:13:45 2016 +0100
 u1 created a.txt
 a.txt | 1 +
 1 file changed, 1 insertion(+)
$ 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
* e684266 - Fri, 26 Feb 2016 10:13:48 +0100 (7 seconds ago) (HEAD -> master)
| u1 appended a.txt - Bartosz Wieczorek
* 7ced2db - Fri, 26 Feb 2016 10:13:49 +0100 (6 seconds ago) (origin/master)
| u2 append 1x b.txt - Bartosz Wieczorek
* d0af369 - Fri, 26 Feb 2016 10:13:45 +0100 (10 seconds ago)
| u1 created b.txt - Bartosz Wieczorek
* fb6668e - Fri, 26 Feb 2016 10:13:45 +01 
  u1 created a.txt - Bartosz Wieczorek

Scenario 3 (both users U1 and U2 are in sync; U1 modified and committed file A; U2 modified committed and pushed file A and B; U1 executes git pull –rebase and gets a conflict for file A –> need to resolve conflict manually, explicitly add resolved file to index and rebase continue, no commit required):

  • U1: create file A and B commits it and pushes to repo
  • U1: modifies A, commits but do not yet push
  • U2: pull changes
  • U2: modifies A and B, commits and pushes
  • U1: pull –rebase# get conflict message
  • U1: resolve conflict and add A to index, rebase continue; no commit required)
#u1
git clone https://github.com/bmwieczorek/git-pull-rebase.git git-pull-rebase-u1
cd git-pull-rebase-u1
echo "u1 created a.txt" > a.txt
git add a.txt
git commit -m "u1 created a.txt"
echo "u1 created b.txt" > b.txt
git add b.txt
git commit -m "u1 created b.txt"
git push origin master
echo "u1 append 1x a.txt" >> a.txt
git add a.txt
git commit -m "u1 appended a.txt" a.txt
cd ..
#u2
git clone https://github.com/bmwieczorek/git-pull-rebase.git git-pull-rebase-u2
cd git-pull-rebase-u2
echo "u2 append 1x a.txt" >> a.txt
git add b.txt
git commit -am "u2 append 1x a.txt"
echo "u2 append 1x b.txt" >> b.txt
git add b.txt
git commit -am "u2 append 1x b.txt"
git push origin master
cd ..
#u1
cd git-pull-rebase-u1
git pull --rebase
$ git pull --rebase
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 6 (delta 0), pack-reused 0
Unpacking objects: 100% (6/6), done.
From https://github.com/bmwieczorek/git-pull-rebase
 5375a97..468d5b6 master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: u1 appended a.txt
Using index info to reconstruct a base tree...
M a.txt
Falling back to patching base and 3-way merge...
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
error: Failed to merge in the changes.
Patch failed at 0001 u1 appended a.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 468d5b6
You are currently rebasing branch 'master' on '468d5b6'.
 (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: a.txt
no changes added to commit (use "git add" and/or "git commit -a")
$ cat a.txt
u1 created a.txt
<<<<<<< 468d5b6fa8d7118166bee3cac511019063179ab4
u2 append 1x a.txt
||||||| merged common ancestors
=======
u1 append 1x a.txt
>>>>>>> u1 appended a.txt
$ vim a.txt
$ cat a.txt
u1 created a.txt
u2 append 1x a.txt
u1 append 1x a.txt
$ git add a.txt
$ git rebase --continue
Applying: u1 appended a.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
 (use "git push" to publish your local commits)
nothing to commit, working directory clean
$ cat a.txt
u1 created a.txt
u2 append 1x a.txt
u1 append 1x a.txt
$ git log --decorate --abbrev-commit --stat | grep -v Author | sed -e /^$/d
commit 50c2e38 (HEAD -> master)
Date: Fri Feb 26 10:27:11 2016 +0100
 u1 appended a.txt
 a.txt | 1 +
 1 file changed, 1 insertion(+)
commit 468d5b6 (origin/master)
Date: Fri Feb 26 10:27:12 2016 +0100
 u2 append 1x b.txt
 b.txt | 1 +
 1 file changed, 1 insertion(+)
commit f0b5706
Date: Fri Feb 26 10:27:12 2016 +0100
 u2 append 1x a.txt
 a.txt | 1 +
 1 file changed, 1 insertion(+)
commit 5375a97
Date: Fri Feb 26 10:27:08 2016 +0100
 u1 created b.txt
 b.txt | 1 +
 1 file changed, 1 insertion(+)
commit 27af46e
Date: Fri Feb 26 10:27:08 2016 +0100
 u1 created a.txt
 a.txt | 1 +
 1 file changed, 1 insertion(+)
$ 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
* 50c2e38 - Fri, 26 Feb 2016 10:27:11 +0100 (3 minutes ago) (HEAD -> master)
| u1 appended a.txt - Bartosz Wieczorek
* 468d5b6 - Fri, 26 Feb 2016 10:27:12 +0100 (3 minutes ago) (origin/master)
| u2 append 1x b.txt - Bartosz Wieczorek
* f0b5706 - Fri, 26 Feb 2016 10:27:12 +0100 (3 minutes ago)
| u2 append 1x a.txt - Bartosz Wieczorek
* 5375a97 - Fri, 26 Feb 2016 10:27:08 +0100 (3 minutes ago)
| u1 created b.txt - Bartosz Wieczorek
* 27af46e - Fri, 26 Feb 2016 10:27:08 +0100 (3 minutes ago)
 u1 created a.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