Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
711 views
in Technique[技术] by (71.8m points)

git rebase upstream/master vs git pull --rebase upstream master

Is there a difference between git rebase upstream/master and git pull --rebase upstream master, and if so, what? The remote could be any remote, not necessarily upstream.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The git pull --rebase will fetch (git fetch) first, updating upstream/master commits.

If you just rebase without first updating upstream/master, you won't get the same result.

I illustrate it in "master branch and 'origin/master' have diverged, how to 'undiverge' branches'?"


SnakE mentions in the comments that git pull --rebase isn't exactly git fetch && git rebase origin/master.
See "what does "git pull --rebase" do?"

(origin/master)
   |
A--B--C (master)
  
  B'--D (actual origin/master after changing B and force pushing)

What git pull --rebase does, in this case, is:

git fetch origin
git rebase --onto origin/master B master

Here:

  • origin/master is the new updated origin/master (B')
  • B is the old origin/master (before a fetch updated it)
  • master is the branch to replay on top of origin/master

This differs from git fetch + git rebase origin/master in that the pull --rebase command tries to find out which commits are really your local ones, and which had come from upstream in an earlier fetch.

To do this, it looks at the reflog of the remote tracking branch (origin/master, in this case). This reflog represents the tips of successive git fetch operations on origin, in "most recent first" order.

For each reflog entry, (origin/master@{1}, then ...{2}, and so on) it checks if that commit is an ancestor of the current branch head master. As soon as it finds one, it picks it as the starting point for the rebase (B in the example above).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...