137

I'm starting to play with Git now and I'm a little bit confused. For me, looks like there are a lot of options to do the same thing. My question for now is what is the difference between the commands below:

  • git remote update
  • git fetch
  • git pull

Also which one is more applicable for update a local copy of a remote branch?

2

4 Answers 4

147

git remote update will update all of your branches set to track remote ones, but not merge any changes in.

git fetch will update only the branch you're on, but not merge any changes in.

git pull will update and merge any remote changes of the current branch you're on. This would be the one you use to update a local branch.

Sign up to request clarification or add additional context in comments.

13 Comments

git fetch will update other branches, if you want. Try git fetch --all
Did you know you can actually do fast-forward merges with fetch by using refspecs?: git fetch origin master:master. That only works if you don't have master checked out though, because Git has to move the branch pointer and won't do it if you're currently on the branch.
'get fetch' seems to fetch all branches when I do it, even without using the --all switch.(Git for windows 2.10.0.1)
This answer has several errors. Mostly they come from confusing the remote tracking branches with regular branches, which is a big mistake. git fetch does not update the "branch you're on". But it does update all remote tracking branches for the remote origin or whatever remote the upstream tracking branch for the current remote is.
Also see the VERY complete answer in stackoverflow.com/a/17512004/994153 which describes how git fetch --all didn't exist at one time, so git remote update what more useful. Now that --all has been added to git fetch, git remote update is not really necessary.
|
11
  • git fetch and git remote update seem to be largely synonymous nowadays. Both are used to update remote tracking branches to a current snapshot of their counterpart on the remote.

    Without any remote or "remote group" specified (and without config caveats), git remote update defaults to behavior identical to git fetch --all (updating the tracking branches for all remotes) but remote update predates the --all flag for fetch.

    Without --all (and without target specified) fetch will be more conservative and only update remote tracking branches from the remote referenced by the upstream of your current local branch. If you have no upstream set for your current branch it will default to "origin".

    If you have only one remote configured and it is called "origin" git fetch and git remote update are equivalent (without config caveats).

    If you are deciding which of these to use, I suggest sticking to fetch for the updating of remote tracking branches and sticking with git remote for changing/listing configuration related to your remotes.

  • git pull is a convenience command that will:

    • perform a fetch
    • check if your currently checked out local branch has an upstream configured, and merge the corresponding (usually identically named) remote tracking branch into the current local one if it does.

I usually try to dissuade new git users from using pull, in favor of performing the merge (or rebase or "hard reset" whatever is appropriate!) themselves. This relies less on magic while mastering the basics.

It really isn't always a good idea to always blindly merge upstream data into your local copy. Especially if your local branch is ahead (prefer rebase) or if remote data was "force pushed" or otherwise changed without simply rolling forward (prefer "hard reset" if you don't have valuable stuff on the local branch).


In regards to remote update and fetch being synonyms (with moderately different default behavior). These are not the only synonyms (or homonyms) in git. It seems git is moving away from the situation where one or two commands do everything depending on flags or subcommands and move towards a (imo superior) situation where distinct functions correspond to different commands. A similar thing happened with git checkout for example. This is a command that does a lot of different things depending on the rest of the arguments. These functions remain, but seem to have gotten specialized alternatives like git switch and git restore.

2 Comments

Good answer, this should be preferred over the top answer by Makoto which has been edited many times and currently has several inaccuracies in it. Note that git pull does not have to merge, if you set either git config --global pull.ff only (only fast forward on pull, no merges or rebase automaticalyl) or git config --global pull.rebase preserve (auto rebase instead of merge on pull, trying to preserve merges) -- these are much better than the default.
@ColinDBennett Ye, but if you are going to deepdive into all non-standard config driven behaviours, we will be here for a while. I believe remote update and fetch also each have their own configs. And if you expect certain config to be there and move to a different system, your workflow is broken. Still better to do a fast-forward merge by hand imo.
0

I don't think the other answer above paint a clear picture with respect to the difference between git remote update and git fetch.

git remote update actually calls git fetch in background, but with specific settings. The --prune option of remote update will override any settings for fetch by explicitly passing --no-prune and checking if you have a remotes.default. Assuming you don't have the latter then the following are the same:

git remote update
# same as
git fetch --no-prune --all
git remote update --prune
# same as
git fetch --prune --all
git remote update origin upstream
# same as
git fetch --no-prune --multiple origin upstream

See the code here and man git-remote for how remotes.default influence the exact command.

This reduces the problem to "What is the difference between git pull and git fetch?" which already has an answer on stackoverflow.

3 Comments

Fetch doesn't prune by default though. Also, looks like there is a prune flag for remote update.
@Fictional Yes, which is exactly what I wrote :) I write about the --prune option of remote update explicitly, and the examples also show that the default of git remote update is exactly that: no pruning. But it explicitly calls fetch with --no-prune because otherwise your git config fetch.prune true setting would apply.
oops. Sorry, I must have missed that middle part.
-8

Not sure about the git remote update, but git pull is the git fetch followed automatically by a git merge...

This is partially a duplicate. Check: What is the difference between 'git pull' and 'git fetch'?

Also, if it means anything to you, I've never used git remote update neither fgit fetch. You can do just fine with pull, commit and push.

Hope it helps..

5 Comments

git fetch has its place...sometimes you only care about how many branches your manager has pruned; sometimes you only want the updates from the branch you're on.
I guess it makes sense =)
I almost never use git pull, I always like to preview any upstream changes before I merge them in, so I mostly use git fetch.
Usually you don't want to merge your local work-in-progress branch to master yet, so it's better to git remote update and git rebase until your work is ready to be merged. Otherwise you'll just end up with a number of useless merge commits in the history.
@user456814 you are a man of culture.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.