Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In git, how do I check out a remote repository's remote branches?

Tags:

git

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
like image 730
Hans Sjunnesson Avatar asked Jun 14 '10 11:06

Hans Sjunnesson


People also ask

How do I display a remote branch?

To view your remote branches, simply pass the -r flag to the git branch command.

How to check out a remote branch in Git?

Follow the steps below to check out a remote branch. Use the Git Fetch command to fetch the remote branch that you want to checkout. We can either mention the name of the remote branch or fetch all the remote branches present in the remote repository. Use the Git Branch command with the -r flag to view the remote-tracking branch names.

How do I see the remote branches of my repository?

To see the remote branches associated with your repository, you need to append the -r flag to the end of the git branch command. In this guide, we discuss how to use the git branch -r command to show remote branches. We also discuss how to use the git remote show command to show branches on the remote version of your repo.

How do I create a local branch from a remote branch?

Simply run git checkout with the name of the remote branch. Git will automatically create a local branch that tracks the remote one: However, if that branch name is found in more than one remote, this won't work as Git doesn't know which to use.

What are remote references in Git?

Remote references are references (pointers) in your remote repositories, including branches, tags, and so on. You can get a full list of remote references explicitly with git ls-remote <remote>, or git remote show <remote> for remote branches as well as more information.


2 Answers

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 with refs/. 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 inside refs/remotes/. But, you could refer to a ref inside it by using a complete ref like refs/remotes/trunk (remotes/trunk might 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/trunk into FETCH_HEAD and check it out as a detached HEAD:

git fetch remote-name-or-url refs/remotes/trunk &&
git checkout FETCH_HEAD

Same, but create a named, local branch instead of using a detached HEAD:

git fetch remote-name-or-url refs/remotes/trunk &&
git checkout -b trunk-from-remote FETCH_HEAD

Same, but directly into a local branch:

git fetch remote-name-or-url refs/remotes/trunk:trunk-from-remote &&
git checkout trunk-from-remote

If you are working with a configured remote, you can rewrite its remote.<remote-name>.fetch configuration and add an extra entry to automate fetching from both refs/heads/ and refs/remotes/.

# fetch branchs of remote into remote-name/heads/*
git config remote.remote-name.fetch '+refs/heads/*:refs/remotes/remote-name/heads/*' &&
# fetch remotes of remote into remote-name/remotes/*
git config --add remote.remote-name.fetch '+refs/remotes/*:refs/remotes/remote-name/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 under refs/remotes/remote-name/.

like image 152
Chris Johnsen Avatar answered Oct 18 '22 14:10

Chris Johnsen


Good question! I know that this works; can't think of anything else that does off the top of my head:

git fetch origin refs/remotes/trunk
git checkout FETCH_HEAD
# or make a branch to check out
git checkout -b remote-trunk FETCH_HEAD

It's odd, by the way, that those remote refs aren't of the form refs/remotes/<remote-name>/<branch-name>... maybe I misunderstood the names, but the method does work.

like image 22
Cascabel Avatar answered Oct 18 '22 15:10

Cascabel