4

I am quite new to Git. I have a bitbucket repo that I need to work on. The workflow is such that I have to create a new branch on the remote, and then pull that down locally. Lets say the remote branch is named new_branch. This what I am doing:

git init
git pull repo new_branch
# edit files
git add .
git commit -m "some changes"

Form here, I am confused as to how to proceed. I am not creating a branch locally, just working on master. I need to push the changes to and only to remote new_branch. Do i do

git push origin new_branch

Unfortunately, this gives me the error

error: src refspec new_branch does not match any.
error: failed to push some refs to 'origin'

Please tell me what I am doing wrong here. Thanks.

Neosapien
  • 157
  • 1
  • 13
  • did you successfully pull the branch you wanted first of all? because if you pulled it then you should be able to push back into it.. the error you’re getting suggests that, that branch doesn’t exist in the remote – mad.meesh Jan 12 '18 at 01:10
  • What does `git remote -v` prints – jstuartmilne Jan 12 '18 at 01:17
  • @mad.meesh yes I was able to pull the branch successfully. I make the changes, commit them, and when I try to push it, it gives me the error. – Neosapien Jan 12 '18 at 09:21
  • @Sudakatux it prints origin repo for both (fetch) and (push) – Neosapien Jan 12 '18 at 09:26

3 Answers3

2

Try instead of creating a new branch after fetching the repo.

git init .
git remote add origin /url/bitbucket/repo
git fetch
git checkout -b new_branch origin/new_branch

# add, commit

git push -u origin new_branch
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
2

After doing

git checkout new_branch
git push origin new_branch

using the remote branch name for both, I got it to work.

Neosapien
  • 157
  • 1
  • 13
  • @VonC Oh yes, thank you! Slight difference though - the -u flag creates a new branch? I followed your procedure step by step but it still giving the same error. It worked when I used the same names for the remote and local branch, though. – Neosapien Jan 12 '18 at 10:23
  • -u is only for the first push: https://stackoverflow.com/a/17096880/6309 – VonC Jan 12 '18 at 10:24
0
git push origin <local_branch>:<remote_branch>

Above command will create a remote branch with name remote_branch, and push the code from local branch as local_branch

Amit Kaneria
  • 5,466
  • 2
  • 35
  • 38