I have a local repository cloned from a bare remote repository. The following command lists al the remote repository’s branches.
$ git ls-remote <remote>
74bd3eb190edb39db04f6c0c4dbbb9e1e96bc6db refs/remotes/test
85de54d6ae813c624b9623983e6b0a4948dae0fe refs/remotes/trunk
I want to checkout and track that remote’s remote branch trunk. How do I do that?
Note that this is different from checking out a remote repository’s local branch.
This is how the remote repository looks like.
$ git branch -a
master
remotes/test
remotes/trunk
After running git fetch < remote > to fetch all of the remote repository’s branches, I get this output in the local repository.
$ git branch -r
repo/HEAD -> repo/master
repo/master
You can fetch any ref from any remote (as long as the server is willing to give it to you). The key to fetching refs outside
refs/heads/is to supply full ref paths that start withrefs/. If desired, you can even pull from repositories that are not configured as remotes (git fetch can take a URL instead of a remote name).By default, configured remotes will only fetch from the remote repository’s
refs/heads/namespace, so they will not pick up anything insiderefs/remotes/. But, you could refer to a ref inside it by using a complete ref likerefs/remotes/trunk(remotes/trunkmight also work, but might also be ambiguous).If the fetched refspec does not specify a destination ref, it will be stored in the special FETCH_HEAD ref.
Fetch a repository’s
refs/remote/trunkinto FETCH_HEAD and check it out as a detached HEAD:Same, but create a named, local branch instead of using a detached HEAD:
Same, but directly into a local branch:
If you are working with a configured remote, you can rewrite its
remote.<remote-name>.fetchconfiguration and add an extra entry to automate fetching from bothrefs/heads/andrefs/remotes/.To avoid possible collisions, the above example configures fetch to store refs into disjoint namespaces (
…/heads/and…/remotes/). You can pick different names if you like. If you are sure there will be no conflicts, you can even stuff them both directly underrefs/remotes/remote-name/.