16

I made a new branch, checked it out and made a commit:

git branch my-branch [HASH]
git checkout my-branch
git commit -am "Add some stuff to my new branch"

However, I can't push it to github. git push origin my-branch returns:

error: src refspec branch does not match any.
error: failed to push some refs to 'https://github.com/Me/my-project.git'

git show-refs looks like this:

[HASH] refs/heads/my-branch
[HASH] refs/heads/master
[HASH] refs/heads/some-branch
[HASH] refs/remotes/origin/master
[HASH] refs/remotes/origin/some-branch
[HASH] refs/stash

What do I need to do?

fredley
  • 32,953
  • 42
  • 145
  • 236

4 Answers4

13

Here is the command you would execute to push all of the changes from your local branch ("my-branch") to a "my-branch" branch on the GitHub repository:

git push -u origin my-branch
Mario Kutlev
  • 4,897
  • 7
  • 44
  • 62
6

The branch doesn't exist on github, when you push git checks the refs of origin for your branch and doesn't find it.

Add the branch as a remote branch:

git 1.8.x

git branch -u origin/my-branch my-branch

git 1.7.x

git branch --set-upstream my-branch origin/my-branch

Now you can push.

Peter van der Does
  • 14,018
  • 4
  • 38
  • 42
4

If you are tired of writing up the branch name, use this neat shorthand command.

git push origin HEAD -u

This basically says: push whatever branch I am currently on to origin and set it as it's upstream (remote tracking branch).

Qwerty
  • 29,062
  • 22
  • 108
  • 136
  • This seems like the easiest way since you don't have to explicitly name the branch. I just did it without the `-u` (e.g. `git push origin HEAD`) and it seems to have worked fine. Do you know what the difference would be? – Mycah Mar 07 '23 at 19:57
  • OK, the local was not tracking the remote until I used the `-u`, then I could actually do a `push`. Thanks. – Mycah Mar 07 '23 at 20:05
0

There is an error in command line parsing. As you can see in this message:

error: src refspec branch does not match any.

git tries to push a branch with the name branch, not my-branch. What OS/shell are you running? Maybe try

git push origin "my-branch"
Chronial
  • 66,706
  • 14
  • 93
  • 99