Andrew C's comment contains the key here, but I'll expound a bit:
git fetch
, with no additional arguments, chooses a remote name by looking at the current branch, or uses origin
(see the documentation for details). Having chosen a remote, it then proceeds as for the next form.
git fetch remote
, again with no additional arguments, uses the given remote, and extracts the fetch =
lines for that remote to obtain a set of "refspecs". It then proceeds similarly to the last case.
git fetch remote refspec
uses the given remote and the given refspec (you may give multiple refspecs here) to choose what references to update.
Once git fetch
has a remote or URL—given the name of a remote, it extracts the url =
line to obtain the URL—it contacts the other git commands on the remote server and asks them for a list of all of the remote repository's references (branches, tags, and other references, all in the refs/*
name-spaces, with a special addition for HEAD
which is also obtainable but not generally used here—it's there for the initial clone step).
For each reference thus obtained, git fetch
sees whether you have asked it to bring that reference over, and if so, what name you asked git to use in your repository.
Again, the names available are obtained from the remote. The names wanted are obtained from your refspecs, and the names they will be given in your repository are also obtained from your refspecs.
A refspec of the form a:b
means "take reference a
, but call it b
locally."
A refspec missing the b
part means "take reference a
, but put it into the special FETCH_HEAD
file." (FETCH_HEAD
then becomes like a normal reference, like MERGE_HEAD
and ORIG_HEAD
and so on, except that it has some extra text written to it meant for the pull
script, so it only sometimes works the way you might expect.)
A refspec may contain a wildcard character: refs/heads/*
means "take all branches" (branches being, by definition, references that begin with refs/heads/
). Normally the fetch =
line in your git configuration says refs/heads/*:refs/remotes/origin/*
.1 As before, this means to rename the matched branch, with the *
on the right expanding to whatever the *
on the left of the colon matched. So this brings all branches over, but renames them as origin/master
and the like. That's normally want you want for a non---bare
repository.
Sometimes that's also what you want for a --bare
repository, and sometimes it's not. In particular, sometimes you want a "mirror" repository, which is a bare clone that simply slave-copies some other repository. To change an ordinary bare repository into such a mirror, you simply need to modify the fetch =
line: instead of refs/heads/*:refs/remotes/origin/*
the line should read fetch = refs/heads/*:refs/heads/*
. In fact, you may want to bring over everything (tags and even notes) with fetch = refs/*:refs/*
. Whether you actually want this is something only you can decide, of course.
Note that this is common enough that git clone
has a flag to set it up automatically: clone with --mirror
and you get a bare clone with the modified fetch =
line.
1Actually the line reads +refs/heads/*:refs/remotes/origin/*
, i.e., there is also a leading +
character. This plus-sign sets the "force flag", as if you had used git fetch --force
, for that particular reference-update. This is not particularly relevant to the spelling issues here, but I'll note that usually you want a forced update for remote branches like the one listed here, and also for pure mirror repositories.
If you're mirroring tags, you probably want those to do forced-update. Ideally, of course, tags never change (nor are deleted) so in an ideal world this would not matter, but in the real world they do sometimes change, or get deleted.
To handle reference deletion, you must tell git fetch
to --prune
(or, similarly, supply --prune
to git remote update
). There is no syntax in refspecs for automatic pruning (although it would be reasonable, I have not seen any proposal for it).