Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: pull vs. fetch→pull [duplicate]

Tags:

I've never been able to get a clear answer to this question.

For a long time, and at the advisement of a coworker, I've been doing this:

git fetch origin
git pull origin <mybranch>

I've been told (and have seen) that git pull does not behave the same way if you do not first do git fetch. You don't get any remote changes.

But all I see online is that git pull is the equivalent of git fetch followed by git merge. If that were true, git pull would include git fetch, and I wouldn't need an explicit git fetch first, right? But that doesn't seem to be the case.

So what I'm looking for is some explicit documentation that describes the observed behavior of git pull. (I know I'll probably also get lots of advice to switch to git fetchgit merge; that's fine too, but I'm really interested in git pull.)

like image 594
John Alexander Avatar asked May 20 '16 16:05

John Alexander


People also ask

What is difference between git fetch and pull and clone?

git fetch is similar to pull but doesn't merge. i.e. it fetches remote updates ( refs and objects ) but your local stays the same (i.e. origin/master gets updated but master stays the same) . git pull pulls down from a remote and instantly merges. git clone clones a repo.

Is git pull and fetch same?

git fetch is the command that tells your local git to retrieve the latest meta-data info from the original (yet doesn't do any file transferring. It's more like just checking to see if there are any changes available). git pull on the other hand does that AND brings (copy) those changes from the remote repository.

Is fetch the same as pull?

When comparing Git pull vs fetch, Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn't make any changes to your local files. On the other hand, Git pull is faster as you're performing multiple actions in one – a better bang for your buck.

Does git pull do a fetch?

The git pull command first runs git fetch which downloads content from the specified remote repository. Then a git merge is executed to merge the remote content refs and heads into a new local merge commit.


1 Answers

We should probably close this as a duplicate, but before that happens, let me see if I can squeeze this in.

While git pull really is git fetch followed by git merge (or git rebase), the precise difference lies in how git pull runs git fetch.

Specifically:

$ git pull

or:

$ git pull remote-name branch-name

(or various similar variants) runs, not plain git fetch, not git fetch remote-name, but git fetch remote-name branch-name.

This has less difference, since Git version 1.8.4, than it did before that version:

  • git fetch origin master unlike git fetch origin or git fetch did not update refs/remotes/origin/master; this was an early design decision to keep the update of remote tracking branches predictable, but in practice it turns out that people find it more convenient to opportunistically update them whenever we have a chance, and we have been updating them when we run git push which already breaks the original "predictability" anyway.

In other words, if git pull decides to run git fetch origin master, this will update origin/master in your repository—but only if you are not running ancient versions of Git such as those included in certain unnamed Linux distributions.

If you run git fetch origin, you will get all remote-tracking branches updated (provided you have a reasonable configuration, which is the default even in said ancient versions of Git). If you run git fetch origin master, you will only get origin/master updated, and again only if your Git is not too ridiculously out of date. Since git pull runs the four-word variant, it updates only one, or even no, remote-tracking branches.

like image 161
torek Avatar answered Oct 15 '22 13:10

torek