3

I would like to clone a branch, make a change and push into the same branch.

I'm doing this:

mkdir myfolder
cd myfolder
git init
git clone "myurl" -b "mybranch"
git remote add origin "myurl"
edit "myfile.txt"
git add "myfile.txt"
git commit -m "my comment"
git push origin "mybranch"

but I got this error:

error: src refspec "mybranch" does not match any 
error: failed to push some refs to "myurl"

what should I do?

mmoya
  • 1,901
  • 1
  • 21
  • 30
John
  • 41
  • 2
  • 2
  • 6
  • Possible duplicate of [src refspec master does not match any when pushing commits in git](http://stackoverflow.com/questions/4181861/src-refspec-master-does-not-match-any-when-pushing-commits-in-git) –  Jun 06 '14 at 04:41

3 Answers3

8

You may explicitly specify to which remote branch you're pushing:

git push origin myBranch:existing_remote_branch

Actually it seems that you perform many excessive steps and generally the workflow is much simpler. Also it's worth to check Configure a local branch for push to specific branch

Update:

Assuming git is relatively modern, I would go as follows:

git clone "$myurl" "$myfolder"
cd "$myfolder"
git checkout "$mybranch"
git remote set-head origin "$mybranch"
... add and commit your changes here ...
git push origin "$mybranch"

I'd like to ask why you created two different git repositories, one in "$myfolder" and another in "$myfolder/<project_name>" ? Is that expected behavior? (I can imagine cases when it may be useful, but they're "corner-cases", at best)

Community
  • 1
  • 1
user3159253
  • 16,836
  • 3
  • 30
  • 56
  • so, also if I clone just a branch, it seem in my local repository I am in master. Should I do `git push origin master:mybranch`, right? what is the simpler workflow? – John Feb 08 '14 at 09:31
  • hi, well that's a good point, thanks a lot for direct me in the right way. I'll have a look and let you know. Thanks a lot for now. – John Feb 08 '14 at 14:18
2

If you want to push to a remote branch with a different name than your local branch, separate the local and remote names with a colon:

git push origin local-name:remote-name
2

I was creating a new repository in GitLab , and got the same error of : git push on branch src refspec does not match any

For me, I Solved by running

git checkout -b main 

then

git push origin main

and that's because when creating a new repository in the default you stand on the MASTER branch , and all the instruction asked to do all action on branch MAIN , so by switching to running all action on branch MAIN, I solved my problem

haddassah S
  • 147
  • 1
  • 7