Git Rebase vs Pull Tutorial
Git Rebase vs Pull Tutorial
We face situations daily where we have to choose between pull and rebase to update the local code with the origin. We will see the difference using an example.
Jul. 08, 20 · Open Source Zone ·
Comment (0)
Join the DZone community and get the full member experience.
We face situations daily where we have to choose between pull and rebase to update the local code with the origin. We will see the difference using an example.
Let’s say we have a master branch and it has only one file Demo.txt.
We add m1 to it and commit it. Later add m2 and commit it and finally add m3 and commit it.
master
Demo.txt
m1 m2 m3
git log
xxxxxxxxxx
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
Author: xyx <xyx@gmail.com>
Date: Tue Jul 7 14:23:25 2020 +0530
m3
commit d1d033fee4dcc9252c57368aa74497d32cebda34
Author: xyx <xyx@gmail.com>
Date: Tue Jul 7 14:22:28 2020 +0530
m2
xxxxxxxxxx
0
xxxxxxxxxx
1
xxxxxxxxxx
2
xxxxxxxxxx
3
Now we checkout feature branch from master and add new feature f1 and commit it. Then we add f2 and commit it.
feature
Demo.txt
xxxxxxxxxx
4
git log
xxxxxxxxxx
5
xxxxxxxxxx
6
xxxxxxxxxx
7
xxxxxxxxxx
8
xxxxxxxxxx
9
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
0
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
1
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
2
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
3
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
4
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
5
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
6
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
7
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
8
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
9
Author: xyx <xyx@gmail.com>
0
Author: xyx <xyx@gmail.com>
1
Author: xyx <xyx@gmail.com>
2
Author: xyx <xyx@gmail.com>
3
Author: xyx <xyx@gmail.com>
4
Author: xyx <xyx@gmail.com>
5
And there will be any more feature branches and their code will be merged to master. Let’s say the master branch has two new commits m4 and m5.
master
git log
Author: xyx <xyx@gmail.com>
6
Author: xyx <xyx@gmail.com>
7
Author: xyx <xyx@gmail.com>
8
Author: xyx <xyx@gmail.com>
9
Date: Tue Jul 7 14:23:25 2020 +0530
0
Date: Tue Jul 7 14:23:25 2020 +0530
1
Date: Tue Jul 7 14:23:25 2020 +0530
2
Date: Tue Jul 7 14:23:25 2020 +0530
3
Date: Tue Jul 7 14:23:25 2020 +0530
4
Date: Tue Jul 7 14:23:25 2020 +0530
5
Date: Tue Jul 7 14:23:25 2020 +0530
6
Date: Tue Jul 7 14:23:25 2020 +0530
7
Date: Tue Jul 7 14:23:25 2020 +0530
8
Date: Tue Jul 7 14:23:25 2020 +0530
9
m3
0
m3
1
m3
2
m3
3
m3
4
m3
5
m3
6
Pull
git pull = git fetch + git merge
git fetch will download new data from a remote repository but it will not integrate any of this new data into your working files.
Now “git pull” will create an extra merge commit in a feature branch for every upstream change, which will pollute the feature branch history.
When we run command git pull origin master, we will see conflicts.
m3
7
After resolving the conflicts and committing the changes.
Commit Messages Order
m3
8
feature
git log
m3
9
commit d1d033fee4dcc9252c57368aa74497d32cebda34
0
commit d1d033fee4dcc9252c57368aa74497d32cebda34
1
commit d1d033fee4dcc9252c57368aa74497d32cebda34
2
commit d1d033fee4dcc9252c57368aa74497d32cebda34
3
commit d1d033fee4dcc9252c57368aa74497d32cebda34
4
commit d1d033fee4dcc9252c57368aa74497d32cebda34
5
commit d1d033fee4dcc9252c57368aa74497d32cebda34
6
commit d1d033fee4dcc9252c57368aa74497d32cebda34
7
commit d1d033fee4dcc9252c57368aa74497d32cebda34
8
commit d1d033fee4dcc9252c57368aa74497d32cebda34
9
Author: xyx <xyx@gmail.com>
0
Author: xyx <xyx@gmail.com>
1
Author: xyx <xyx@gmail.com>
2
Author: xyx <xyx@gmail.com>
3
Author: xyx <xyx@gmail.com>
4
Author: xyx <xyx@gmail.com>
5
Author: xyx <xyx@gmail.com>
6
Author: xyx <xyx@gmail.com>
7
Author: xyx <xyx@gmail.com>
8
Author: xyx <xyx@gmail.com>
9
Date: Tue Jul 7 14:22:28 2020 +0530
0
Date: Tue Jul 7 14:22:28 2020 +0530
1
Date: Tue Jul 7 14:22:28 2020 +0530
2
Date: Tue Jul 7 14:22:28 2020 +0530
3
Date: Tue Jul 7 14:22:28 2020 +0530
4
Date: Tue Jul 7 14:22:28 2020 +0530
5
Date: Tue Jul 7 14:22:28 2020 +0530
6
Date: Tue Jul 7 14:22:28 2020 +0530
7
Date: Tue Jul 7 14:22:28 2020 +0530
8
Date: Tue Jul 7 14:22:28 2020 +0530
9
m2
0
m2
1
m2
2
Rebase
To avoid merge commit, we use rebasing. With rebasing we re-write the history. Rebase puts the entire feature branch on tip of master.
It creates new commits for each commit in the original branch.
So if you already pushed your commits to the remote repository, you will need to use –force after rebasing because the commit hash is completely changed. f1 and f2 have different commit hashes.
git rebase -i origin/master will show conflicts, resolve the conflicts, and run git rebase –continue. When all the conflicts will be resolved, we will see:
m2
3
Commit Messages Order
m2
4
feature
git log
m2
5
m2
6
m2
7
m2
8
m2
9
xxxxxxxxxx
00
xxxxxxxxxx
01
xxxxxxxxxx
02
xxxxxxxxxx
03
xxxxxxxxxx
04
xxxxxxxxxx
05
xxxxxxxxxx
06
xxxxxxxxxx
07
xxxxxxxxxx
08
xxxxxxxxxx
09
xxxxxxxxxx
10
xxxxxxxxxx
11
xxxxxxxxxx
12
xxxxxxxxxx
13
xxxxxxxxxx
14
xxxxxxxxxx
15
xxxxxxxxxx
16
xxxxxxxxxx
17
xxxxxxxxxx
18
xxxxxxxxxx
19
xxxxxxxxxx
20
xxxxxxxxxx
21
xxxxxxxxxx
22
xxxxxxxxxx
23
History Rewritten — Here the commit hashes of f1 and f2 have changed from 614709283fa14a4f3bb730b2b0b80ea397b8c9a1 to 4b7991c9aee5f30b1922ff190c9b5b3296563aa8 and cd9982e7a3692805a6bb487075641755422836d1 to c65c3387ca3bc51333c7c4c3d6e25495a251a5a4 respectively.
Thanks for reading!!!
Comment (0)
Published at DZone with permission of Jyoti Sachdeva . See the original article here.
Opinions expressed by DZone contributors are their own.
Open Source Partner Resources
ABOUT US
CONTRIBUTE ON DZONE
LEGAL
CONTACT US
- 600 Park Offices Drive
- Suite 150
- Research Triangle Park, NC 27709
- support@dzone.com
- +1 (919) 678-0300
- {{ node.blurb }}
{{ editionName }}
{{ parent.title || parent.header.title}}
{{ parent.tldr }}
{{ parent.linkDescription }}
{{ message }}
{{ $dialog.title }}