5

I don't understand the advantage. I've seen it argued in places such as this article that fetching instead of pulling gives you the opportunity to inspect the changes before you merge them into your local branch.

But, in that article for instance, the author uses git diff master origin/master to inspect the changes made in the newly fetched branch by comparing it to his local master.

This is where I get confused....that command would work anyway, regardless of whether you had fetched first. Either way it's comparing local master to remote master. You don't need to fetch to compare your local branch to the remote branch.

What am I missing?

temporary_user_name
  • 35,956
  • 47
  • 141
  • 220

2 Answers2

10

You're missing that git fetch is used to update your local copy of the remote branch.

So the command you mentioned would do a diff between the local branch master and the local copy of origin/master, not the actual state of the master branch on origin. fetch will connect to the remote server and download all the changes of the remote (changes in branches, tags, ...).

As always, the git manual on Branching and Remote Branches explains it well.

achedeuzot
  • 4,164
  • 4
  • 41
  • 56
  • Whoa. Did not know that. For real? Without fetching there's no way to diff against the remote branch? You have to have a "local version" of the remote branch? – temporary_user_name Apr 08 '14 at 19:25
  • Because git is a distributed CVS, your local git repository will have it's local branches (`master` by default). But if you work with one or more remote servers, git will make a local copy of the state of these servers. They may have some of your local branches or different ones. I don't know of any way to make a diff against a remote repo directly... (at least not in git itself). – achedeuzot Apr 08 '14 at 19:31
  • 2
    In order to diff against a remote repo, you have to download the state of that repo. Network operations can be slow. Very slow In order to not incur the penalty of a network operation *every* time you run a diff, git only does the download when explicitly requested. That's what `git fetch` does. – BJ Homer Apr 08 '14 at 19:39
-4

fetch is a lower-level command; pull is implemented in terms of it, for instance. You probably won't get much advantage out of using it directly in most cases.

  • I strongly disagree with this statement. `git fetch` is very useful (see the accepted answer for details) and I advise people to use it (in conjunction with `git diff` and `git merge`) instead of `git pull` in virtually all cases. – ChrisGPT was on strike Apr 08 '14 at 21:05
  • I also wouldn't agree that `fetch` is "lower-level" than `pull`. There are [two "levels" of commands](http://stackoverflow.com/questions/6976473/what-does-the-term-porcelain-mean-in-git) in Git, and `fetch` and `pull` are both "porcelain", not "plumbing". – ChrisGPT was on strike Apr 08 '14 at 21:08