3

One creates a new git branch:

git checkout -b test

and pushed it to origin:

git push origin -u test

and listing all the branches now I get:

  master
* test
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/test

Now, on another machine I want execute a command to get the remote branch refs. How can I do this?

I want to have the output:

* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/test

And the command IS NOT AWARE of the name of the new branches.

I have tried: git pull, git pull --all, git fetch, git fetch --all. What else?

EDIT: I need a command that tells me:

* [new branch]      test       -> origin/test

Is this possible WITHOUT checking out a branch first? I also don't want to remove my repo and clone it again.

Gabriel Petrovay
  • 20,476
  • 22
  • 97
  • 168

3 Answers3

4

on another machine I want execute a command to get the remote branch refs.

You can initiate an empty repo, add an origin and do a ls-remote (as done in the comments of your other question):

git init newRepo
cd newRepo
git remote add origin /url/remote/repo
git ls-remote origin

That allows you to see the branches of the remote repo without having to clone / fetch anything locally.
And since you are in an empty repo, you can then decide what to fetch.


As jthill mentions in the comments, the git ls-remote part doesn't need to be run from a git repo.
As an example, consider:

cd / # to illustrate that you can be anywhere
git ls-remote git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git 'refs/heads/*'

If you have already a cloned repo, and don't mind to download the delta, then a git fetch should work, provided that its refspec is set to fetch all branches.
(Again, as shown in your other question "git checkout new remote branch when cloning with depth 1 option")

git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    `git ls-remote` works from anywhere, you don't need to init a throwaway. `( cd /; git ls-remote git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git 'refs/heads/*'; )` – jthill May 06 '14 at 02:00
  • @jthill True. I have included your comment in the answer for more visibility. – VonC May 06 '14 at 05:23
0

To list the remote branches of a local repository, in this case it's

$ git branch --remote |grep origin

In your case, to update remote refs of a repo corresponding to another remote repo called origin, it's

$ git fetch origin

This will create a new remote branch origin/test if it was created on the remote repo. You can then make it a local branch and check it out with

git checkout --track origin/test
CharlesB
  • 86,532
  • 28
  • 194
  • 218
0

if you already have a git repo and just want to track a new branch from remote, you can use:

git checkout --track origin/test

if you want to clone a perticular branch:

git clone -b test <remote location>
suhailvs
  • 20,182
  • 14
  • 100
  • 98