Git fetch remote branch to new local branch (with one command)

I have a local repo with * master tracking remote A * master. (Remote A * master is the only active branch.)

Remote B now has * prbranch which has additional work not in A / master. In my local repo there is no reference to any remote other than A (and my personal remote that is only used for pushing).

How can I a) checkout B / prbranch into my local repo and b) create a new local branch with the same name and update automatically?

git fetch B prbranch

fetches the branch correctly, but puts it in FETCH_HEAD, with which I have to manually create a branch with a similar name.

git pull B prbranch

is of course useless trying to merge into the current local branch

and git clone --single-branch B prbranch

just creates a new repo in the subfolder of the current repo.

So how can I get the remote branch AND create an identical local branch in one command? Of course, it's not hard to do ...

(My experience is of course limited, so I expect the answer to be simple and "retroactively obvious".)

+3


source to share


1 answer


The following command will fetch and create a local branch without creating a remote tracking branch:

git fetch <urlB> refs/heads/prbranch:refs/heads/prbranch

      



See Git Internals - Refspec in the documentation.

+1


source







All Articles