Why include `master` in the command `git fetch upstream master`?
Question
In the section Pulling in upstream changes on help.github's Forking a project it states:
Some time has passed, the upstream repo has changed and you want to update your fork before you submit a new patch. There are two ways to do this:
$ git fetch upstream master
$ git merge upstream/master
Why are they including master
in the fetch command? I've looked at the git help fetch
information but I'm not understanding what including master
does. Thanks.
Solution
That allows you to:
- update your local version of the master upstream branch only (as opposed as updating all branches of the upstream repo, which can be longer to do)
- no trigger a merge right away (as opposed to the pull command)
The git merge
will then try to merge that local version of the upstream master to your repo master branch.
So here, master is, for the fetch
command, a refspec.
<refspec>
The format of a
<refspec>
parameter is an optional plus +, followed by the source ref<src>
, followed by a colon :, followed by the destination ref<dst>
.The remote ref that matches
<src>
is fetched, and if<dst>
is not empty string, the local ref that matches it is fast-forwarded using<src>
.
If the optional plus+
is used, the local ref is updated even if it does not result in a fast-forward update.
Here, <dst>
is empty, so the matching local branch (your master) is updated.
Without master, that would give:
git fetch upstream
The above command copies all branches from the remote
refs/heads/
namespace and stores them to the localrefs/remotes/upstream/
namespace, unless thebranch.<name>.fetch
option is used to specify a non-default refspec.