Git: How to push from "no branch" to a new remote branch?
Asked Answered
R

2

9

I did:

git co upstream/master  # To no branch.
<then did a trivial edit on some file...>
git commit -a -m "Trivial edit"
git push origin NewBranch

But got this:

$ git push origin ignore-netbeans-config
error: src refspec ignore-netbeans-config does not match any.

Can I push to a new branch without creating a local one?

Rimini answered 10/11, 2011 at 16:26 Comment(1)
Good question! Of course, if you might do any further editing here, you'd want to go ahead and create a branch to be safe.Reverend
A
10

Let's clear up a few small details first:

  1. A branch is a "ref" under the refs/heads namespace. Just ls .git/refs/heads and cat the files there to see what I mean.
  2. A tag is "ref" under the refs/tags namespace. Just ls .git/refs/tags to see for yourself.
  3. HEAD just another "ref", but it's special in the aspect that it can be "symbolic". Just cat .git/HEAD and see what it says.

A push operation operates on a "ref", and the default "mapping" preserves the namespace. This means that when I push a branch, it'll appear as a branch on the remote; when I push a tag, it'll appear as a tag on the remote. Consider the following scenarios:

  1. I want to push the tag moo and make it appear as a branch on the remote server (yes, I'm essentially "converting" a tag into a branch). This is how I'll do it:

    git push origin moo:refs/heads/moo

  2. Git needs a way to differentiate between fast-forward and non-ff pushes, so that people don't end up overwriting other people's work by mistake. Let's say I want to push the branches master, next, and pu, of which only pu is non-ff. This is how I'll do it (note that you must supply an explicit mapping when you're using +):

    git push origin master next +pu:pu

  3. Now, let's get to your question. You want to push your HEAD so that it appears in the refs/heads namespace on the remote as a branch named "ignore-netbeans-config". If this branch didn't exist before, or if you're overwriting some commits in it (ie. non-ff push), use the +. Otherwise, don't. End result:

    git push origin +HEAD:refs/heads/ignore-netbeans-config

TL;DR version: git push origin +HEAD:refs/heads/ignore-netbeans-config

Andris answered 11/11, 2011 at 17:12 Comment(0)
S
11

try

git push origin HEAD:refs/heads/ignore-netbeans-config
Statecraft answered 10/11, 2011 at 16:33 Comment(2)
You should be able to omit the "refs/heads/" part of it.Latty
I guess if it doesn't exist yet you have to be explicit.Latty
A
10

Let's clear up a few small details first:

  1. A branch is a "ref" under the refs/heads namespace. Just ls .git/refs/heads and cat the files there to see what I mean.
  2. A tag is "ref" under the refs/tags namespace. Just ls .git/refs/tags to see for yourself.
  3. HEAD just another "ref", but it's special in the aspect that it can be "symbolic". Just cat .git/HEAD and see what it says.

A push operation operates on a "ref", and the default "mapping" preserves the namespace. This means that when I push a branch, it'll appear as a branch on the remote; when I push a tag, it'll appear as a tag on the remote. Consider the following scenarios:

  1. I want to push the tag moo and make it appear as a branch on the remote server (yes, I'm essentially "converting" a tag into a branch). This is how I'll do it:

    git push origin moo:refs/heads/moo

  2. Git needs a way to differentiate between fast-forward and non-ff pushes, so that people don't end up overwriting other people's work by mistake. Let's say I want to push the branches master, next, and pu, of which only pu is non-ff. This is how I'll do it (note that you must supply an explicit mapping when you're using +):

    git push origin master next +pu:pu

  3. Now, let's get to your question. You want to push your HEAD so that it appears in the refs/heads namespace on the remote as a branch named "ignore-netbeans-config". If this branch didn't exist before, or if you're overwriting some commits in it (ie. non-ff push), use the +. Otherwise, don't. End result:

    git push origin +HEAD:refs/heads/ignore-netbeans-config

TL;DR version: git push origin +HEAD:refs/heads/ignore-netbeans-config

Andris answered 11/11, 2011 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.