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?
-
13Source for people advising against git pull --rebase? Rebase or git rebase is separate activity from git pull --rebase!przemo_li– przemo_li2018-04-17 12:49:21 +00:00Commented Apr 17, 2018 at 12:49
11 Answers
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?
13 Comments
git fetch && git rebase explicitly, does it do the same thing as git pull --rebase?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 Comments
git config --global pull.rebase preserve (preserve says in addition to enabling rebasing, to try to preserve merges if you have made any locally).Perhaps the best way to explain it is with an example:
- Alice creates topic branch A, and works on it
- Bob creates unrelated topic branch B, and works on it
- Alice does
git checkout master && git pull. Master is already up to date. - Bob does
git checkout master && git pull. Master is already up to date. - Alice does
git merge topic-branch-A - Bob does
git merge topic-branch-B - Bob does
git push origin masterbefore Alice - Alice does
git push origin master, which is rejected because it's not a fast-forward merge. - Alice looks at origin/master's log, and sees that the commit is unrelated to hers.
- Alice does
git pull --rebase origin master - Alice's merge commit is unwound, Bob's commit is pulled, and Alice's commit is applied after Bob's commit.
- 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 Comments
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.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 Comments
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 Comments
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.
9 Comments
pull --rebase should always be used, why doesn't pull do it by default?.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.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-rebaseEdit:
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 --forceit (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.
6 Comments
pull --rebase you overwrite the remote branch (origin) and your local branch and worktree as well. (Yes, the version that contained your commit is available from the reflog but you'll have to somehow extract those changes.)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.
Comments
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 Comments
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.
Comments
I think the usage of git pull --rebase is only for when more than one person is working on the same branch. In this scenario, if you should use it or not, will depend on the defined standards of the company or team you are working with. git pull will create a merge commit which can act as a tag, and in some scenarios this can be a desired thing. git pull --rebase won't do that, and it can be the case that the company/team defines this in search of a cleaner/linear commit history.
git pull --rebase can be used to update your local branches when you are working in a team and someone else merges stuff, e.g. in develop or master branches.
Personally, I never use git pull at all - I go for the non-atomic, two-step solution. being on the branch I want to update I execute: git fetch + git rebase origin/name-of-the-branch. might be a bit archaic, but I know exactly how these commands work. I guess git pull --rebase is basically executing these two commands, but if you have conflicts you will have to fix and use git rebase commands i.e. git rebase --continue, git rebase --abort. so for people that needs to learn git, it's better to have each step well defined.
1 Comment
git pull since 2016. When I was in school and worked as a TA helping students who were very new to git, I would tell them that and wait for them to wonder "that makes no sense, how do you get code that others have pushed?" and then I tell them I just do git fetch look around, then do git merge or git rebase or something else depending on the situation and my objectives.