Git Rebase vs Pull Tutorial – DZone Open Source | xxxGit Rebase vs Pull Tutorial – DZone Open Source – xxx
菜单

Git Rebase vs Pull Tutorial – DZone Open Source

五月 30, 2020 - MorningStar

Over a million developers have joined DZone.

  • Git Rebase vs Pull Tutorial - DZone Open Source

    {{node.title}}

    {{node.type}} · {{ node.urlSource.name }} by

    Download {{node.downloads}}

  • {{totalResults}} search results

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

= 1024)” dc-slot=”ads.sl1.slot(articles[0], 0)” tags=”ads.sl1.tags(articles[0], 0)” size=”ads.sl1.size(articles[0], 0)” style=”border:0;”>
1 && !articles[0].partner.isSponsoringArticle && (width >= 1024)” dc-slot=”ads.sb2.slot(articles[0], 0)” tags=”ads.sb2.tags(articles[0], 0)” size=”ads.sb2.size(articles[0], 0)”>

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.

Jul. 08, 20 · Open Source Zone ·

Free Resource

Join the DZone community and get the full member experience.

Join For Free

Git Rebase vs Pull Tutorial - DZone Open Source

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

xxxxxxxxxx

0

10

xxxxxxxxxx

1

11

xxxxxxxxxx

2

12

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

Shell

 

xxxxxxxxxx

5

1

20

 

1

xxxxxxxxxx

6

2

xxxxxxxxxx

7

3

xxxxxxxxxx

8

4

xxxxxxxxxx

9

5

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

0

6

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

1

7

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

2

8

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

3

9

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

4

10

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

5

11

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

6

12

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

7

13

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

8

14

commit 1c5cff83d7fdf0d857a57e84b0f363b422604800 (HEAD -> master, origin/master)

9

15

Author: xyx  <xyx@gmail.com>

0

16

Author: xyx  <xyx@gmail.com>

1

17

Author: xyx  <xyx@gmail.com>

2

18

Author: xyx  <xyx@gmail.com>

3

19

Author: xyx  <xyx@gmail.com>

4

20

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

Shell

 

Author: xyx  <xyx@gmail.com>

6

1

20

 

1

Author: xyx  <xyx@gmail.com>

7

2

Author: xyx  <xyx@gmail.com>

8

3

Author: xyx  <xyx@gmail.com>

9

4

Date:   Tue Jul 7 14:23:25 2020 +0530

0

5

Date:   Tue Jul 7 14:23:25 2020 +0530

1

6

Date:   Tue Jul 7 14:23:25 2020 +0530

2

7

Date:   Tue Jul 7 14:23:25 2020 +0530

3

8

Date:   Tue Jul 7 14:23:25 2020 +0530

4

9

Date:   Tue Jul 7 14:23:25 2020 +0530

5

10

Date:   Tue Jul 7 14:23:25 2020 +0530

6

11

Date:   Tue Jul 7 14:23:25 2020 +0530

7

12

Date:   Tue Jul 7 14:23:25 2020 +0530

8

13

Date:   Tue Jul 7 14:23:25 2020 +0530

9

14

    m3

0

15

    m3

1

16

    m3

2

17

    m3

3

18

    m3

4

19

    m3

5

20

    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

Shell

 

    m3

9

1

33

 

1

commit d1d033fee4dcc9252c57368aa74497d32cebda34

0

2

commit d1d033fee4dcc9252c57368aa74497d32cebda34

1

3

commit d1d033fee4dcc9252c57368aa74497d32cebda34

2

4

commit d1d033fee4dcc9252c57368aa74497d32cebda34

3

5

commit d1d033fee4dcc9252c57368aa74497d32cebda34

4

6

commit d1d033fee4dcc9252c57368aa74497d32cebda34

5

7

commit d1d033fee4dcc9252c57368aa74497d32cebda34

6

8

commit d1d033fee4dcc9252c57368aa74497d32cebda34

7

9

commit d1d033fee4dcc9252c57368aa74497d32cebda34

8

10

commit d1d033fee4dcc9252c57368aa74497d32cebda34

9

11

Author: xyx <xyx@gmail.com>

0

12

Author: xyx <xyx@gmail.com>

1

13

Author: xyx <xyx@gmail.com>

2

14

Author: xyx <xyx@gmail.com>

3

15

Author: xyx <xyx@gmail.com>

4

16

Author: xyx <xyx@gmail.com>

5

17

Author: xyx <xyx@gmail.com>

6

18

Author: xyx <xyx@gmail.com>

7

19

Author: xyx <xyx@gmail.com>

8

20

Author: xyx <xyx@gmail.com>

9

21

Date:   Tue Jul 7 14:22:28 2020 +0530

0

22

Date:   Tue Jul 7 14:22:28 2020 +0530

1

23

Date:   Tue Jul 7 14:22:28 2020 +0530

2

24

Date:   Tue Jul 7 14:22:28 2020 +0530

3

25

Date:   Tue Jul 7 14:22:28 2020 +0530

4

26

Date:   Tue Jul 7 14:22:28 2020 +0530

5

27

Date:   Tue Jul 7 14:22:28 2020 +0530

6

28

Date:   Tue Jul 7 14:22:28 2020 +0530

7

29

Date:   Tue Jul 7 14:22:28 2020 +0530

8

30

Date:   Tue Jul 7 14:22:28 2020 +0530

9

31

    m2

0

32

    m2

1

33

    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

Shell

 

    m2

5

1

28

 

1

    m2

6

2

    m2

7

3

    m2

8

4

    m2

9

5

xxxxxxxxxx

00

6

xxxxxxxxxx

01

7

xxxxxxxxxx

02

8

xxxxxxxxxx

03

9

xxxxxxxxxx

04

10

xxxxxxxxxx

05

11

xxxxxxxxxx

06

12

xxxxxxxxxx

07

13

xxxxxxxxxx

08

14

xxxxxxxxxx

09

15

xxxxxxxxxx

10

16

xxxxxxxxxx

11

17

xxxxxxxxxx

12

18

xxxxxxxxxx

13

19

xxxxxxxxxx

14

20

xxxxxxxxxx

15

21

xxxxxxxxxx

16

22

xxxxxxxxxx

17

23

xxxxxxxxxx

18

24

xxxxxxxxxx

19

25

xxxxxxxxxx

20

26

xxxxxxxxxx

21

27

xxxxxxxxxx

22

28

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

Like This Article? Read More From DZone

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.

Open Source Partner Resources

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

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

by

CORE

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}


Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51