15

I am working on a branch "branch-A" and using the command git branch I can see that its the only branch locally.

If I use git branch -a I can see the remote copy of "remotes/origin/branch-B"

What I want to do is bring branch-B locally, but I don't want to actually check it out... I mean I could do that and then checkout my other branch to go back, but its slightly more painful since I am doing this to lots of repos.

I was thinking some sort of fetch? but I can't figure out how to phrase the command. Is it possible?

So I have:

user@pc> git branch
* branch-A
  master

and

user@pc> git branch -a
* branch-A
  remotes/origin/branch-B
  remotes/origin/branch-A
  remotes/origin/master

I want to be able to get:

user@pc> git branch
* branch-A
  branch-B
  master
code_fodder
  • 15,263
  • 17
  • 90
  • 167

5 Answers5

26

You can do this, one liner very simple.

git fetch <remote> <srcBranch>:<destBranch>

this will avoid to checkout the remote branch.

See this question for more information:

Merge, update, and pull Git branches without using checkouts

Community
  • 1
  • 1
danglingpointer
  • 4,708
  • 3
  • 24
  • 42
  • Yep, this does exactly what it says on the tin : )) I was kind of hoping that `git fetch ` would do it and default a local branch name of the same name, but it does not seem to work... oh well, its not a massive effort to repeat the name - but I don't really get the ":" syntax, don't really use that much with git (at least I don't so far). Nice answer :) – code_fodder Mar 30 '17 at 06:17
  • 1
    i think git-completion.bash can do this and much more helpful stuff https://github.com/git/git/blob/master/contrib/completion/git-completion.bash – rome Mar 14 '19 at 17:55
1

You can run a script to create new local branches with the name of all remote branches.

$ git fetch
$ for branch in `git branch -r | sed 's@origin/@ @'`;do `git branch  $branch origin/$branch`;done

git branch -r shows all the remote branches
sed 's@origin/@ @' split the origin/ from the begining of branch name
git branch $branch origin/$branch create a new branch with the history of origin/branch

Now see all local branches

$ git branch
Sajib Khan
  • 22,878
  • 9
  • 63
  • 73
  • hehe, this is quite clever. Mostly I am interested in the "git branch origin/ part, that particular use of git branch is new to me. It is equivalent to the one in that I marked up as the "answer"... +1 for that part. – code_fodder Mar 30 '17 at 06:38
1

I think there's some ambiguity about what "bring branch-B locally" means.

If you see origin/branch-B then you already have it locally. The commits, the trees they contain, the files they contain... all are already in your local repo. The only thing you don't have is a local branch (branch-B) tracking the remote branch (origin/branch-B).

If you don't want to check branch-B out, then there aren't a lot of reasons to create the local branch. Just about anything you can do with a local branch, you can do with the remote branch reference. e.g.:

git merge origin/branch_B

But if you want one, you can create it:

git branch --set-upstream branch_B origin/branch_B
Mark Adelsberger
  • 42,148
  • 4
  • 35
  • 52
  • Good points raised here, thanks for the explanation - it helps :) I was, in the end starting to use the full path "remotes/origin/..." and I could achieve what I wanted this way. But I still want to know how to do what I asked for simplicity/convenience reasons. +1 anyway. – code_fodder Mar 30 '17 at 06:20
0
git branch branch-B origin/branch-B

After a fetch or pull. You can also change the local name; however, I can't think of a case where a branch just needs to lie around except for checkout or if you intend to clone from this copy. Stuff like git log origin/branch-B..branch-A or git merge origin/branch-B work just fine. You can check out also, but only in a detached HEAD state.

chipfall
  • 300
  • 2
  • 6
-1

The command you're searching for is git fetch and use the option --all to fetch all remote branches.

Akram Fares
  • 1,653
  • 2
  • 17
  • 32
  • hmm.... no, this does not seem to do what I want, I mean I am sure it updates the information for all branches, but I don't get the result of a local tracking branch :( – code_fodder Mar 30 '17 at 06:34