1

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,656
  • 1
  • 18
  • 26
John
  • 21
  • 1
  • 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

2 Answers2

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
  • 14,913
  • 3
  • 24
  • 43
  • 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