Git Rebase vs Pull

Git first cover
Reading Time: 4 minutes

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

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

commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:20:47 2020 +0530

    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

commit cd9982e7a3692805a6bb487075641755422836d1 (HEAD -> feature, origin/feature)
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:26:30 2020 +0530

    f2

commit 614709283fa14a4f3bb730b2b0b80ea397b8c9a1
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:25:47 2020 +0530

    f1

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (origin/master, 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

commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:20:47 2020 +0530

    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


commit 4b4721676d426d57e101e11eae206172521ab25e (HEAD -> master, origin/master)
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:29:31 2020 +0530

    m5

commit d9eebbeb632df371013644c8f21fbe9580973402
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:27:51 2020 +0530

    m4

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800
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

commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:20:47 2020 +0530

    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

commit 1ea10e0ee2ad5a47c23e237f21d38002fc838c0e (HEAD -> feature)
Merge: cd9982e 4b47216
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:34:13 2020 +0530

    merged with master

commit 4b4721676d426d57e101e11eae206172521ab25e (origin/master, master)
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:29:31 2020 +0530

    m5

commit d9eebbeb632df371013644c8f21fbe9580973402
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:27:51 2020 +0530

    m4

commit cd9982e7a3692805a6bb487075641755422836d1 (origin/feature)
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:26:30 2020 +0530

    f2

commit 614709283fa14a4f3bb730b2b0b80ea397b8c9a1
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:25:47 2020 +0530

    f1

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800
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

commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
Author: xyx <xyx@gmail.com>
Date:   Tue Jul 7 14:20:47 2020 +0530

    m1

Rebase

To avoid merge commit, we use rebasing. With rebasing we re-write the history. Rebase puts 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.
Rebasing feature branch using git rebase -i origin/master

Commit Messages Order

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

feature

git log

commit c65c3387ca3bc51333c7c4c3d6e25495a251a5a4 (HEAD -> feature)
Author: xyz <xyz@gmail.com>
Date:   Tue Jul 7 14:26:30 2020 +0530

    f2

commit 4b7991c9aee5f30b1922ff190c9b5b3296563aa8
Author: xyz <xyz@gmail.com>
Date:   Tue Jul 7 14:25:47 2020 +0530

    f1

commit 4b4721676d426d57e101e11eae206172521ab25e (origin/master, master)
Author: xyz <xyz@gmail.com>
Date:   Tue Jul 7 14:29:31 2020 +0530

    m5

commit d9eebbeb632df371013644c8f21fbe9580973402
Author: xyz <xyz@gmail.com>
Date:   Tue Jul 7 14:27:51 2020 +0530

    m4

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800
Author: xyz <xyz@gmail.com>
Date:   Tue Jul 7 14:23:25 2020 +0530

    m3

commit d1d033fee4dcc9252c57368aa74497d32cebda34
Author: xyz <xyz@gmail.com>
Date:   Tue Jul 7 14:22:28 2020 +0530

    m2

commit ba9bce8ffff7e6dadf81597a063df677a87d6eaf
Author: xyz <xyz@gmail.com>
Date:   Tue Jul 7 14:20:47 2020 +0530

    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!!!

Footer

Written by 

Jyoti Sachdeva is a software consultant with more than 6 months of experience. She likes to keep up with the trending technologies. She is familiar with languages such as C,C++,Java,Scala and is currentky working on akka,akka http and scala. Her hobbies include watching tv series and movies, reading novels and dancing.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading