Search code examples
gitbranchfetch

How do you fetch a remote branch without "checking it out"


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

Solution

  • 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