Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I push my new branch?

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?

like image 748
fredley Avatar asked Dec 06 '12 12:12

fredley


People also ask

How do I push after creating a new branch?

Push a new Git branch to a remote repo Clone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch.

How do I force a branch to push another branch?

In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.

How do I push to an existing branch?

To push the branch or you can say to push the changes in the branch to the Github repo you have to run this command “git push origin <the branch name>” in our case the branch name is “main”. After pushing the changes the repo will look like and this is how you can push a branch to a remotely hosted GitHub repository.


2 Answers

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
like image 50
Mario Kutlev Avatar answered Sep 23 '22 00:09

Mario Kutlev


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.

like image 30
Peter van der Does Avatar answered Sep 23 '22 00:09

Peter van der Does