Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

branch

fetch

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
like image 249
code_fodder Avatar asked Mar 29 '17 13:03

code_fodder


People also ask

How do I fetch a particular branch remotely?

If you have a single remote repository, then you can omit all arguments. just need to run git fetch , which will retrieve all branches and updates, and after that, run git checkout <branch> which will create a local copy of the branch because all branches are already loaded in your system.

How do you check that the branch is tracking the remote branch?

1 Answer. There is a command that gives you about all tracking branches. And to know about the pull and push configuration per branch you can use the command git remote show origin. and you can use -sb option for seeing the upstream.

Which command can be used to track a remote branch?

You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".


2 Answers

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

like image 95
danglingpointer Avatar answered Nov 15 '22 22:11

danglingpointer


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
like image 30
Mark Adelsberger Avatar answered Nov 15 '22 20:11

Mark Adelsberger