Reputation: 1434
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:
Also which one is more applicable for update a local copy of a remote branch?
Upvotes: 130
Views: 107809
Reputation: 2153
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.
Upvotes: 0
Reputation: 484
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:
fetch
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
.
Upvotes: 5
Reputation: 106430
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.
Upvotes: 139
Reputation: 190
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..
Upvotes: -8