Is ‘git pull’ really broken? I know what you are thinking; such a pervasive and basic command cannot possibly be broken. Unfortunately, it is.
It is not some marginal issue, many experienced Git users avoid ‘git pull’ and even urge newcomers to avoid using that command, there’s many sites that encourage you to not use the command, and there have been a lot of threads on the mailing list about the issue (Pull is mostly evil, A failing attempt to use Git in a centralized environment), the maintainer, Junio C Hamano has accepted there’s a big problem, even Linus Torvalds agreed something needs to change.
In order to identify the problem we first need to define the two main ways ‘git pull’ is used.
One way ‘git pull’ is used, is to integrate pull requests into the mainline. For example in the Linux kernel, the DRM maintainer sends a pull request to Linus Torvalds, saying basically:
The following changes are available in the git repository at:
So Linus can just do:
git pull git://people.freedesktop.org/~airlied/linux drm-next
In this mode ‘git pull’ actually works fine, which is not too surprising, since it’s the main thing Linus Torvalds does.
However, this is not the way most people use ‘git pull’.
What most people do is for example update their local ‘master’ branch, to the remote ‘origin/master’ branch. Essentially doing ‘git fetch origin’, ‘git merge origin/master’.
However, that’s not exactly what most people actually want to do.
If you don’t have any changes of your own in ‘master’, then yes, ‘git pull’ does what you want, but if you do have changes, and thus the branches have diverged, then ‘git pull’ will create a new merge commit. This might or might not be what you want, but the majority of Git newbies do not want that, or rather, the team they contribute to don’t want those “evil merges”. Unfortunately these newbies don’t know what they are doing, and Git is not making it easier.
So you end up with something like this:
Most likely what the team wants is that the local chances are rebased on top of the remote ones, but if they want a merge, they want it the other way around, that is: merge the local changes to the remote ones, as if a topic branch was merged.
A merge with this order of parents has many advantages, including a clearer history, however, it’s not possible to do that with ‘git pull’, so you have to do ‘git fetch’, create a new branch, switch to the master branch, merge the other branch, and finally remove the other branch. It’s not straight-forward at all.
It is this mode that is broken, and that’s the reason many people try to avoid ‘git pull’; it rarely does what you want by default.
There have been many solutions proposed, however, there are many many use-cases to consider, and a solution that takes them all into consideration for the future is not easy to find.
The best solution that seems to accommodate all present use-cases and future ones is the introduction of a new command: ‘
By default this command will complain if the branches have diverged, so you have to either do ‘
git update --rebase‘ or ‘
git update --merge‘, this ensures that newbies aren’t going to do “evil merges” by mistake.
Also, when you do a ‘
git update --merge‘ the order of the parents is reversed, which means it appears you are merging ‘master’ to ‘origin/master’, and not the other way around as it happens with ‘git pull’, which means it appears as if you are merging a topic branch, which is what most people want.
There are many many more advantages to this new command, but probably too subtle to mention in this post.
When will this be ready?
Probably never. I sent a summary of the issues and the solution to the mailing list, which addresses all the use-cases that were discussed. I have the required patches with tests and documentation on my personal branch, and I’ve been using this new command for a while now.
Why isn’t this picked? Maybe it’s because none of the core developers experience these issues. Maybe because they don’t use ‘git pull’ in the second form. Who knows.
The fact is that there is no interest to get this fixed, even though the issue has been acknowledged, so it’s not likely to be fixed any time soon.
So what can you do about it? The best thing you can do right now is simply avoid using ‘git pull’. Additionally, you might want to instruct your fellow coworkers to avoid unsing it as well, specially the ones that are not very familiar with Git.
Also, you might want to use my fork, git-fc, which does have the ‘
git update‘ command, which works better than ‘
git pull‘ even when there’s no branch divergence, and when there is, ‘
git update --merge‘ is also superior, because the order of the parents is right.