Question

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?

Was it helpful?

Solution

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

OTHER TIPS

try

git push origin HEAD:refs/heads/ignore-netbeans-config
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top