Explosion Pills
Explosion Pills

Reputation: 191749

Fetching and checkout a remote branch without detached head

I fetch a remote branch via git fetch origin name-of-branch:refs/remotes/name-of-branch.

I use git checkout name-of-branch I am put in a detached head state. I can get to a named branch now by using git checkout -b some-branch-name. Is there any way to checkout the remote branch to a named branch (of the same name even) in one command?

If I use git checkout -b name-of-remote-branch (i.e. use the same name), then checkout another branch, then check that branch out again git checkout name-of-remote-branch, I get

warning: refname 'name-of-remote-branch' is ambiguous.

However, this doesn't happen if I use git pull first and then use checkout:

Branch name-of-pulled-branch set up to track remote branch name-of-pulled-branch from origin

I assume that a lot of my woes with respect to the fetched branch have to do with the fact that it is not set up to track the remote branch. Why is this, and is there any way to checkout the fetched branched and have it set up to track the remote branch as if I had used git pull?

Upvotes: 3

Views: 2396

Answers (1)

stellarhopper
stellarhopper

Reputation: 670

You want:

git fetch origin name-of-branch:refs/remotes/name-of-branch
git checkout -b some-branch-name name-of-remote-branch --track

This will set up tracking so that git pull/push will Just Work.

Upvotes: 2

Related Questions