Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a remote Git branch without creating a local branch?

I'd like to create a new remote branch for later use. The most commonly suggested way seems to be:

git checkout -b newbranch origin/startingpoint
git push origin newbranch

But this will also create the branch locally and put me on it. Is there a way of creating a remote branch without creating it locally and moving onto it?

like image 888
Steve Chambers Avatar asked Nov 27 '13 13:11

Steve Chambers


1 Answers

tl;dr: specify the full destination refname and you can push a commit directly:

git push origin origin/startingpoint:refs/heads/newbranch

you can push anything that resolves to an id, including just an id:

git push origin 6a5343d:refs/heads/newbranch

The jargon for what git push operates on is a "refspec", a mapping of source objects to destination names. If you don't supply a complete refspec, Git fills in defaults from what you did supply. It's good enough that relatively few people ever even need to know it's happening.

So "master" generally gets resolved as a branch name (most people instinctively avoid punning branch and tag names and Git caters to people with those instincts) and you can specify however much of the refname-typing prefix you need to disambiguate; when you say git push origin newbranch git resolves the local newbranch ref by the linked procedure and then fills in the destination ref for you.

like image 52
jthill Avatar answered Oct 14 '22 14:10

jthill