62

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.

Dennis
  • 56,821
  • 26
  • 143
  • 139

1 Answers1

68

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).

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 14
    So `git pull --rebase upstream master` is analogous to `git fetch upstream && git rebase upstream/master`? – Dennis May 10 '13 at 04:01
  • 6
    Actually no. Imagine you pulled history `A-B` and made a change on top of it, `A-B-C`. Then somebody else amended `B` to `B'` and foce-pushed their changes so that the origin is now `A-B'-D`. Now if you do `git fetch && git rebase origin/master` the rebase will fail with conflicts. However `git pull --rebase` will figure it out and end up with `A-B'-D-C`. Some magic is definitely happening under the carpet in `pull --rebase`. Edit: [prooflink](http://gitolite.com/git-pull--rebase.html) – SnakE Mar 17 '15 at 18:34
  • @SnakE good point, that you. I have included the `pull --rebase` algo in the answer for more visibility. – VonC Mar 17 '15 at 18:54
  • 2
    The magic is available directly in `git merge-base --fork-point` – jthill Mar 17 '15 at 21:10
  • @jthill since got 1.9.0+, I presume, since I documented fork-point there: http://stackoverflow.com/a/20423029/6309 – VonC Mar 17 '15 at 21:13
  • @VonC What is the use of the argument `B` in `git rebase --onto origin/master B master`? Git manual says it's ``, but what would that mean when `--onto` option is specified? – stillanoob Jul 31 '18 at 05:11
  • @ibrahim5253 `B` is the first commit (included) you are rebasing on top of the branch referenced by `--onto`. – VonC Jul 31 '18 at 06:33