1159

I know of some people who use git pull --rebase by default and others who insist never to use it. I believe I understand the difference between merging and rebasing, but I'm trying to put this in the context of git pull. Is it just about not wanting to see lots of merge commit messages, or are there other issues?

1
  • 10
    Source for people advising against git pull --rebase? Rebase or git rebase is separate activity from git pull --rebase!
    – przemo_li
    Apr 17, 2018 at 12:49

10 Answers 10

976

I would like to provide a different perspective on what git pull --rebase actually means, because it seems to get lost sometimes.

If you've ever used Subversion (or CVS), you may be used to the behavior of svn update. If you have changes to commit and the commit fails because changes have been made upstream, you svn update. Subversion proceeds by merging upstream changes with yours, potentially resulting in conflicts.

What Subversion just did, was essentially git pull --rebase. The act of re-formulating your local changes to be relative to the newer version is the "rebasing" part of it. If you had done svn diff prior to the failed commit attempt, and compare the resulting diff with the output of svn diff afterwards, the difference between the two diffs is what the rebasing operation did.

The major difference between Git and Subversion in this case is that in Subversion, "your" changes only exist as non-committed changes in your working copy, while in Git you have actual commits locally. In other words, in Git you have forked the history; your history and the upstream history has diverged, but you have a common ancestor.

In my opinion, in the normal case of having your local branch simply reflecting the upstream branch and doing continuous development on it, the right thing to do is always --rebase, because that is what you are semantically actually doing. You and others are hacking away at the intended linear history of a branch. The fact that someone else happened to push slightly prior to your attempted push is irrelevant, and it seems counter-productive for each such accident of timing to result in merges in the history.

If you actually feel the need for something to be a branch for whatever reason, that is a different concern in my opinion. But unless you have a specific and active desire to represent your changes in the form of a merge, the default behavior should, in my opinion, be git pull --rebase.

Please consider other people that need to observe and understand the history of your project. Do you want the history littered with hundreds of merges all over the place, or do you want only the select few merges that represent real merges of intentional divergent development efforts?

11
  • 31
    @MarceloCantos To be clear, I am not saying that git (the tool) should default to rebase. That would be dangerous since a rebase essentially destroys history. I am saying that in terms of workflow, when you are not intending to branch and are just hacking away at some branch that other people are also working on, "git pull --rebase" should be the default behavior of the user.
    – scode
    Oct 1, 2011 at 8:12
  • 12
    @MarceloCantos No I'm not ;) Personally I would leave autosetupmerge ast the default of true (if I'm merging back and fourth between branches other than local<->remote, I like it to be explicit). I am just saying that as a human, I always use "git pull --rebase" as part of my normal "grab latest in master branch" workflow, because I never want to create a merge commit unless I am explicitly branching.
    – scode
    Oct 2, 2011 at 10:58
  • 20
    +1 @scode. After many painful hours of grappling with the rebase/merge question, finally here's an answer that nails it.
    – Thinkisto
    Oct 16, 2012 at 5:55
  • 15
    This answer is just an attempt to adapt non-distributed version control systems users to Git instead of giving them a chance to properly understand the benefits of having proper branches. Sep 23, 2013 at 7:51
  • 3
    Awesome! I work in a company where the strategy is cherry-pick not merge nor pull request, therefore the local branch must reflect the remote and we should always "update" the local branch before pushing to avoid conflicts in the code-review tool. I always use git fetch && git rebase explicitly, does it do the same thing as git pull --rebase?
    – Thomas H.
    Apr 9, 2019 at 20:27
742

You should use git pull --rebase when

  • your changes do not deserve a separate branch

Indeed -- why not then? It's more clear, and doesn't impose a logical grouping on your commits.


Ok, I suppose it needs some clarification. In Git, as you probably know, you're encouraged to branch and merge. Your local branch, into which you pull changes, and remote branch are, actually, different branches, and git pull is about merging them. It's reasonable, since you push not very often and usually accumulate a number of changes before they constitute a completed feature.

However, sometimes--by whatever reason--you think that it would actually be better if these two--remote and local--were one branch. Like in SVN. It is here where git pull --rebase comes into play. You no longer merge--you actually commit on top of the remote branch. That's what it actually is about.

Whether it's dangerous or not is the question of whether you are treating local and remote branch as one inseparable thing. Sometimes it's reasonable (when your changes are small, or if you're at the beginning of a robust development, when important changes are brought in by small commits). Sometimes it's not (when you'd normally create another branch, but you were too lazy to do that). But that's a different question.

7
  • 25
    I think it is useful when you work on the same branch, but you may change your workstation. I tend to commit and push my changes from one workstation then pull rebase in the other, and keep working on the same branch. Aug 3, 2015 at 23:38
  • 14
    It is a useful best practice to set up Git to automatically rebase upon pull with git config --global pull.rebase preserve (preserve says in addition to enabling rebasing, to try to preserve merges if you have made any locally). Apr 8, 2016 at 19:24
  • 4
    I disagree that you should use pull --rebase only when working on one branch. You should use it all the time, unless impossible because of some other circumstance. Jun 27, 2018 at 14:28
  • 1
    @P Shved... 'However, sometimes--by whatever reason--you think that it would actually be better if these two--remote and local--were one branch'...can this be done?My understadning is that in local environment, I can have my branch and a remote branch mirror as origin/master. Can yuo please provide inputs here?
    – Mandroid
    Jul 18, 2018 at 12:11
  • 2
    I think it's worth noting that conflicts will result in a merge anyway.
    – ka3ak
    Dec 23, 2020 at 8:28
430

Perhaps the best way to explain it is with an example:

  1. Alice creates topic branch A, and works on it
  2. Bob creates unrelated topic branch B, and works on it
  3. Alice does git checkout master && git pull. Master is already up to date.
  4. Bob does git checkout master && git pull. Master is already up to date.
  5. Alice does git merge topic-branch-A
  6. Bob does git merge topic-branch-B
  7. Bob does git push origin master before Alice
  8. Alice does git push origin master, which is rejected because it's not a fast-forward merge.
  9. Alice looks at origin/master's log, and sees that the commit is unrelated to hers.
  10. Alice does git pull --rebase origin master
  11. Alice's merge commit is unwound, Bob's commit is pulled, and Alice's commit is applied after Bob's commit.
  12. Alice does git push origin master, and everyone is happy they don't have to read a useless merge commit when they look at the logs in the future.

Note that the specific branch being merged into is irrelevant to the example. Master in this example could just as easily be a release branch or dev branch. The key point is that Alice & Bob are simultaneously merging their local branches to a shared remote branch.

6
  • 6
    Nice. I tend to be explicit and git co master && git pull; git checkout topic-branch-A; git rebase master; git checkout master; git merge topic-branch-A; git push origin master and repeat if another's push to master happened before mine. Though i can see the succinct advantages in your recipe.
    – HankCa
    Dec 19, 2017 at 23:16
  • 7
    "Alice's commit is applied after Bob's commit" perhaps worth noting the commit hash of Alice's commit does change in this case.
    – jiggunjer
    Feb 4, 2021 at 10:51
  • 1
    Literally best possible explanation of the following topic
    – John
    Dec 2, 2022 at 14:38
  • 2
    I don't understand. If the content modified by two people is not related, there should be no conflict between the modified content. Why is push not allowed in this case?
    – alalalala
    Feb 22, 2023 at 2:43
  • 3
    @alalalala When these branches were made, they were based on HEAD of master. When branch-B is merged, the HEAD of master changed. If B's branch had 5 commits, then the previous HEAD that branch-A knows about becomes HEAD-5 after B's merge. So now, branch-A does not know where it goes. It only knows for sure it goes after HEAD-5, but now there are 5 other commits! Does it go after HEAD or somewhere before HEAD and after HEAD-5? Doing rebase answers the question, saying: "you go after HEAD." It is no longer "based" on (old) HEAD-5. It is now "based" on (new) HEAD. Hence: rebase(d)!
    – Zim
    Oct 19, 2023 at 0:26
190

I think you should use git pull --rebase when collaborating with others on the same branch. You are in your work → commit → work → commit cycle, and when you decide to push your work your push is rejected, because there's been parallel work on the same branch. At this point I always do a pull --rebase. I do not use squash (to flatten commits), but I rebase to avoid the extra merge commits.

As your Git knowledge increases you find yourself looking a lot more at history than with any other version control systems I've used. If you have a ton of small merge commits, it's easy to lose focus of the bigger picture that's happening in your history.

This is actually the only time I do rebasing(*), and the rest of my workflow is merge based. But as long as your most frequent committers do this, history looks a whole lot better in the end.

(*) While teaching a Git course, I had a student arrest me on this, since I also advocated rebasing feature branches in certain circumstances. And he had read this answer ;) Such rebasing is also possible, but it always has to be according to a pre-arranged/agreed system, and as such should not "always" be applied. And at that time I usually don't do pull --rebase either, which is what the question is about ;)

6
  • 4
    surely one can write a script to hide the merge commits from the log
    – hasen
    Mar 18, 2010 at 22:03
  • 4
    Merges can contain diffs too, which means it's not entirely trivial
    – krosenvold
    Mar 19, 2010 at 5:52
  • 11
    @hasen j Yes, but the CONTENTS of those merges may matter
    – krosenvold
    Mar 19, 2010 at 22:09
  • This answer is vague and opinionated in comparison with the chosen answer: what do you mean by "same branch" exactly? There are some good points, that are not in the chosen answer however.
    – iwein
    Jun 5, 2011 at 12:32
  • 2
    The vagueness around "branch" is quite intentional, since there are so many ways to use refs; "line of work" is just one option.
    – krosenvold
    Jun 5, 2011 at 19:38
72

Just remember:

  • pull = fetch + merge
  • pull --rebase = fetch + rebase

So, choose the way what you want to handle your branch.

You'd better know the difference between merge and rebase :)

2
  • while mnemonically helpful this is not strictly true
    – CervEd
    Jun 17, 2021 at 15:57
  • 14
    this doesn't address the question of when to use rebase. Nov 14, 2021 at 2:38
59

I don't think there's ever a reason not to use pull --rebase -- I added code to Git specifically to allow my git pull command to always rebase against upstream commits.

When looking through history, it is just never interesting to know when the guy/gal working on the feature stopped to synchronise up. It might be useful for the guy/gal while he/she is doing it, but that's what reflog is for. It's just adding noise for everyone else.

8
  • 3
    "When looking through history, it is just never interesting to know when the guy working on the feature stopped to sync up." / but doesn't that mean those intermediate commits are likely broken builds?
    – eglasius
    Sep 24, 2011 at 21:37
  • 15
    Yes, and they're also not a "whole state". That's why we don't want them. I want to know what he wanted, not how he got there.
    – Dustin
    Sep 25, 2011 at 22:09
  • 8
    If pull --rebase should always be used, why doesn't pull do it by default?
    – straya
    Nov 1, 2016 at 5:54
  • 3
    I'm afraid you're going to have to form your own opinion. I've got several things in my .gitconfig to make some of the options do the right thing. I think git rebase does the wrong thing by default, as does git tag, etc... If you disagree, you don't need to justify your opinion.
    – Dustin
    Nov 7, 2016 at 16:31
  • 13
    That seems good advice if you pull from "upstream", say from master, and if the branch you pull into hasn't gone public (yet). If you pull on the other hand from a feature branch into master it's more like the other way around: there's never a reason to use --rebase, right? That might be the reason why it is not default. I found Rebases are how changes should pass from the top of hierarchy downwards and merges are how they flow back upwards. derekgourlay.com/blog/git-when-to-merge-vs-when-to-rebase
    – jolvi
    Nov 17, 2016 at 1:18
22

Edit: Please do not use --rebase as a default as suggested by some other answers, you could lose some of your work. Example:

  • A collaborator pushes commit 1
  • On the same branch, you push commit 2
  • The collaborator, without pulling your work, amends commit 1 and git push --force it (not a nice thing to do, but you are not in control here)
  • If you git pull --rebase, you will silently lose commit 2 with all the work it contained.

I recommend using git pull --rebase only if you know you forgot to push your commits before someone else does the same.

If you did not commit anything, but your working space is not clean, just git stash before git pull, then git stash pop. This way you won't silently rewrite your history which could silently drop some of your work.

4
  • How would it drop work?
    – user18099
    Jun 26, 2022 at 8:42
  • 5
    @user18099 let's say you (A) and somebody else (B) are working on the same branch. B pushes their commit 1, you push your commit 2, and B, without pulling your work, amends their commit 1 and then push force. If you pull rebase, you will lose commit 2 with all the work it contained.
    – Habax
    Jun 27, 2022 at 12:33
  • 2
    That's a great rule of thumb—stash before pulling Nov 15, 2022 at 15:58
  • 3
    You do not "silently lose commit 2" in the scenario you described. The diff between commit 2 and the amended commit 1 from origin is still applied locally during the rebase steps. You don't lose your work.
    – Tony B
    Jun 3, 2023 at 5:02
14

I think it boils down to a personal preference.

Do you want to hide your silly mistakes before pushing your changes? If so, git pull --rebase is perfect. It allows you to later squash your commits to a few (or one) commits. If you have merges in your (unpushed) history, it is not so easy to do a git rebase later one.

I personally don't mind publishing all my silly mistakes, so I tend to merge instead of rebase.

3
  • 17
    Note to anyone viewing this question: this answer is completely irrelevant to the question "when should I use git pull --rebase?" Jan 18, 2014 at 17:24
  • 4
    @therealklanni I edited the answer so it gets clearer how it is relevant to the question (hopefully without destroying the intent). Jan 30, 2014 at 12:53
  • 3
    Sharing a dirty and unstructured work-log is not a honest endeavor, it's just laziness. You're wasting peoples time by making them chase you through your rabbit hole of development and debugging; give them the result, not the ramblings.
    – krystah
    Apr 9, 2020 at 20:50
11

This scenario describes a situation where you cannot push your commits because the code on origin is changed. It is easy to understand Cody' s explanation. I draw a diagram to describe the scenario and hope it is helpful. If I am wrong, please correct me.

enter image description here

6

One practice case is when you are working with Bitbucket PR. Let's understand it with the below case:

There is PR open.

Then you decided to rebase the PR remote branch with the latest Master branch through BitBucket GUI. This action will change the commits' ids of your PR.

Since you have rebased the remote branch using GUI. first, you have to sync the local branch on your PC with the remote branch.

In this case git pull --rebase works like magic.

After git pull --rebase your local branch and remote branch have same history with the same commit ids.

Then now if you add a new commit/changes to the PR branch.

You can nicely push a new commit without using force or anything to remote branch/BitBucket PR.

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.