338

Another question says that git pull is like a git fetch + git merge.

But what is the difference between git pull and git fetch + git rebase?

3
  • 2
    someone should clean up the link... and I'm amazed at how many votes that other question got. Commented Jul 28, 2010 at 20:33
  • 13
    @xeno: I think its just a count of how many people go "I had this question too"
    – bobobobo
    Commented Jul 28, 2010 at 21:10
  • 59
    Some day I'll find time to really read the git documentation, but until then, I'm adding my votes to these types of questions
    – Eran Medan
    Commented Sep 18, 2012 at 16:03

2 Answers 2

374

It should be pretty obvious from your question that you're actually just asking about the difference between git merge and git rebase.

So let's suppose you're in the common case - you've done some work on your master branch, and you pull from origin's, which also has done some work. After the fetch, things look like this:

- o - o - o - H - A - B - C (master)
               \
                P - Q - R (origin/master)

If you merge at this point (the default behavior of git pull), assuming there aren't any conflicts, you end up with this:

- o - o - o - H - A - B - C - X (master)
               \             /
                P - Q - R --- (origin/master)

If on the other hand you did the appropriate rebase, you'd end up with this:

- o - o - o - H - P - Q - R - A' - B' - C' (master)
                          |
                          (origin/master)

The content of your work tree should end up the same in both cases; you've just created a different history leading up to it. The rebase rewrites your history, making it look as if you had committed on top of origin's new master branch (R), instead of where you originally committed (H). You should never use the rebase approach if someone else has already pulled from your master branch.

Finally, note that you can actually set up git pull for a given branch to use rebase instead of merge by setting the config parameter branch.<name>.rebase to true. You can also do this for a single pull using git pull --rebase.

16
  • 40
    What happens if you were to rebase after someone had already pulled from your master branch? Would that break the repo?
    – Didier A.
    Commented Mar 22, 2011 at 6:36
  • 14
    How do you know if someone has pulled from your master branch?
    – Frank
    Commented Aug 7, 2012 at 5:47
  • 33
    If you don't know for sure that someone hasn't, you should assume that they have.
    – Chris Down
    Commented Feb 11, 2013 at 6:20
  • 4
    I was just thinking that unless you're also pushing changes somewhere other than origin/master, I don't see ever running into the problem of someone else having pulled the changes in question, because if you had already pushed these changes to origin/master, there would be nothing to rebase in the first place. It seems to me that the warning really only matters in cases of where you've got something more complex than X -> origin/X, but I could be wrong. If someone knows of a scenario I'm overlooking, please share.
    – neverfox
    Commented Jun 26, 2013 at 17:00
  • 1
    @SteveChambers No, that's not the result. The lines simply represent commit ancestry, i.e. A is the parent of B. There's not any implication about whether Q or B was first in time. All these operations are based on commit graphs, not time. Rebase simply transplants some commits, with the result as I showed no matter what the commit timestamps are.
    – Cascabel
    Commented Nov 6, 2013 at 14:57
35

TLDR:

git pull is like running git fetch then git merge
git pull --rebase is like git fetch then git rebase

In reply to your first statement,

git pull is like a git fetch + git merge.

"In its default mode, git pull is shorthand for git fetch followed by git merge FETCH_HEAD" More precisely, git pull runs git fetch with the given parameters and then calls git merge to merge the retrieved branch heads into the current branch"

(Ref: https://git-scm.com/docs/git-pull)


For your second statement/question:

'But what is the difference between git pull VS git fetch + git rebase'

Again, from same source:
git pull --rebase

"With --rebase, it runs git rebase instead of git merge."


Now, if you wanted to ask

'the difference between merge and rebase'

that is answered here too:
https://git-scm.com/book/en/v2/Git-Branching-Rebasing
(the difference between altering the way version history is recorded)

3
  • 4
    I'd like to mention that "git pull --rebase" is like "git fetch then git rebase" most of the time - but not always. In some situations, "git pull --rebase" does a bit more. See this often referenced example here: gitolite.com/git-pull--rebase
    – Daniel K.
    Commented Jul 22, 2019 at 14:20
  • 1
    Thank you so much for your answer. I really understand the way git fetch + git rebase commands work from now. There's no more or less conflict on our git tree from now :)
    – David
    Commented Nov 7, 2019 at 7:17
  • 1
    @DanielK, git rebase also does those additional steps, since shortly after that blog post was written. See the discussion of --fork-point in git help rebase.
    – Buster
    Commented Jan 15, 2021 at 16:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.