{{announcement.body}}
{{announcement.title}}

Git Rebase vs Pull Tutorial

DZone 's Guide to

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.

· Open Source Zone ·
Free Resource

git logo

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

Shell
 




xxxxxxxxxx
1
12


1
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)
2
Author: xyx  <xyx@gmail.com>
3
Date:   Tue Jul 7 14:23:25 2020 +0530
4
    m3
5
commit d1d033fee4dcc9252c57368aa74497d32cebda34
6
Author: xyx <xyx@gmail.com>
7
Date:   Tue Jul 7 14:22:28 2020 +0530
8
    m2
9
commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
10
Author: xyx <xyx@gmail.com>
11
Date:   Tue Jul 7 14:20:47 2020 +0530
12
    m1


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

m1
m2
m3
f1
f2

git log

Shell
 




xxxxxxxxxx
1
20


1
commit cd9982e7a3692805a6bb487075641755422836d1 (HEAD -> feature, origin/feature)
2
Author: xyx <xyx@gmail.com>
3
Date:   Tue Jul 7 14:26:30 2020 +0530
4
    f2
5
commit 614709283fa14a4f3bb730b2b0b80ea397b8c9a1
6
Author: xyx <xyx@gmail.com>
7
Date:   Tue Jul 7 14:25:47 2020 +0530
8
    f1
9
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (origin/master, master)
10
Author: xyx <xyx@gmail.com>
11
Date:   Tue Jul 7 14:23:25 2020 +0530
12
    m3
13
commit d1d033fee4dcc9252c57368aa74497d32cebda34
14
Author: xyx <xyx@gmail.com>
15
Date:   Tue Jul 7 14:22:28 2020 +0530
16
    m2
17
commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
18
Author: xyx <xyx@gmail.com>
19
Date:   Tue Jul 7 14:20:47 2020 +0530
20
    m1


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

Shell
 




xxxxxxxxxx
1
20


1
commit 4b4721676d426d57e101e11eae206172521ab25e (HEAD -> master, origin/master)
2
Author: xyx <xyx@gmail.com>
3
Date:   Tue Jul 7 14:29:31 2020 +0530
4
    m5
5
commit d9eebbeb632df371013644c8f21fbe9580973402
6
Author: xyx <xyx@gmail.com>
7
Date:   Tue Jul 7 14:27:51 2020 +0530
8
    m4
9
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800
10
Author: xyx <xyx@gmail.com>
11
Date:   Tue Jul 7 14:23:25 2020 +0530
12
    m3
13
commit d1d033fee4dcc9252c57368aa74497d32cebda34
14
Author: xyx <xyx@gmail.com>
15
Date:   Tue Jul 7 14:22:28 2020 +0530
16
    m2
17
commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
18
Author: xyx <xyx@gmail.com>
19
Date:   Tue Jul 7 14:20:47 2020 +0530
20
    m1


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.

From https://github.com/jyotisachdeva57/git-pull-rebase-demo
 * branch            master     -> FETCH_HEAD
Auto-merging Demo.txt
CONFLICT (content): Merge conflict in Demo.txt
Automatic merge failed; fix conflicts and then commit the result.

After resolving the conflicts and committing the changes.

Commit Messages Order

m1 -> m2 -> m2 -> f1 -> f2 -> m4 -> m5 -> merged with master

feature

git log

Shell
 




xxxxxxxxxx
1
33


1
commit 1ea10e0ee2ad5a47c23e237f21d38002fc838c0e (HEAD -> feature)
2
Merge: cd9982e 4b47216
3
Author: xyx <xyx@gmail.com>
4
Date:   Tue Jul 7 14:34:13 2020 +0530
5
    merged with master
6
commit 4b4721676d426d57e101e11eae206172521ab25e (origin/master, master)
7
Author: xyx <xyx@gmail.com>
8
Date:   Tue Jul 7 14:29:31 2020 +0530
9
    m5
10
commit d9eebbeb632df371013644c8f21fbe9580973402
11
Author: xyx <xyx@gmail.com>
12
Date:   Tue Jul 7 14:27:51 2020 +0530
13
    m4
14
commit cd9982e7a3692805a6bb487075641755422836d1 (origin/feature)
15
Author: xyx <xyx@gmail.com>
16
Date:   Tue Jul 7 14:26:30 2020 +0530
17
    f2
18
commit 614709283fa14a4f3bb730b2b0b80ea397b8c9a1
19
Author: xyx <xyx@gmail.com>
20
Date:   Tue Jul 7 14:25:47 2020 +0530
21
    f1
22
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800
23
Author: xyx <xyx@gmail.com>
24
Date:   Tue Jul 7 14:23:25 2020 +0530
25
    m3
26
commit d1d033fee4dcc9252c57368aa74497d32cebda34
27
Author: xyx <xyx@gmail.com>
28
Date:   Tue Jul 7 14:22:28 2020 +0530
29
    m2
30
commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
31
Author: xyx <xyx@gmail.com>
32
Date:   Tue Jul 7 14:20:47 2020 +0530
33
    m1


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:

[detached HEAD 4b7991c] f1
 1 file changed, 1 insertion(+), 2 deletions(-)
Successfully rebased and updated refs/heads/feature.

Commit Messages Order

m1 -> m2 -> m2 -> m4 -> m5 -> f1 -> f2

feature

git log

Shell
 




xxxxxxxxxx
1
28


 
1
commit c65c3387ca3bc51333c7c4c3d6e25495a251a5a4 (HEAD -> feature)
2
Author: xyz <xyz@gmail.com>
3
Date:   Tue Jul 7 14:26:30 2020 +0530
4
    f2
5
commit 4b7991c9aee5f30b1922ff190c9b5b3296563aa8
6
Author: xyz <xyz@gmail.com>
7
Date:   Tue Jul 7 14:25:47 2020 +0530
8
    f1
9
commit 4b4721676d426d57e101e11eae206172521ab25e (origin/master, master)
10
Author: xyz <xyz@gmail.com>
11
Date:   Tue Jul 7 14:29:31 2020 +0530
12
    m5
13
commit d9eebbeb632df371013644c8f21fbe9580973402
14
Author: xyz <xyz@gmail.com>
15
Date:   Tue Jul 7 14:27:51 2020 +0530
16
    m4
17
commit 1c5cff83d7fdf0d857a57e84b0f363b422604800
18
Author: xyz <xyz@gmail.com>
19
Date:   Tue Jul 7 14:23:25 2020 +0530
20
    m3
21
commit d1d033fee4dcc9252c57368aa74497d32cebda34
22
Author: xyz <xyz@gmail.com>
23
Date:   Tue Jul 7 14:22:28 2020 +0530
24
    m2
25
commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
26
Author: xyz <xyz@gmail.com>
27
Date:   Tue Jul 7 14:20:47 2020 +0530
28
    m1


History Rewritten — Here the commit hashes of f1 and f2 have changed from 614709283fa14a4f3bb730b2b0b80ea397b8c9a1 to 4b7991c9aee5f30b1922ff190c9b5b3296563aa8 and cd9982e7a3692805a6bb487075641755422836d1 to c65c3387ca3bc51333c7c4c3d6e25495a251a5a4 respectively.

Thanks for reading!!!

Topics:
git and github, git beginners guide, version control, version control system

Published at DZone with permission of Jyoti Sachdeva . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}