How to create a remote Git branch without creating a local branch?
I would like to make twalberg's comment an answer. You can create a remote branch by adding refs/head/
to it:
git push origin origin/startingpoint:refs/heads/newbranch
As torek explains, git push
adds refs/heads/
automatically if you have a local branch. If you don't have a local branch, it doesn't know if you want to push to refs/heads/
or to refs/tags/
. My version of git says: "we are unable to guess a prefix based on the source ref". By adding refs/heads/
you tell git to create a branch on the remote, not a tag.
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.