Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have my local repo with *master tracking remote A's *master. (Remote A's *master is its only active branch.)

Remote B now has a *prbranch that has some extra work not in A/master. My local repo doesn't have any reference to any remote besides A (and my personal remote that is only used for pushing).

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

git fetch B prbranch correctly fetches the branch, but puts it into FETCH_HEAD with which I have to manually create a like-named branch.

git pull B prbranch is of course useless, trying to merge into the current local branch

and git clone --single-branch B prbranch just makes a new repo in a subfolder of the current repo.

So how can I fetch the remote branch AND create an identical local branch in one command? Surely this isn't hard to do...

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

like image 764
Dubslow Avatar asked Dec 31 '25 09:12

Dubslow


1 Answers

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 - The Refspec on the documentation.

like image 96
Ed I Avatar answered Jan 03 '26 07:01

Ed I