38

I could use a hand with learning how to push a local branch to a remote branch. Please see below. Help much appreciated!

The local branch was created after cloning the repo then doing

$ git checkout -b mybranch remotes/origin/mybranch

$ git branch -a
  master
* mybranch
  remotes/origin/HEAD -> origin/master
  remotes/origin/master
  remotes/origin/mybranch

But when trying to push changes back up:

$ git push mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

$ git push remotes/origin/mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

$ git push origin/mybranch mybranch
fatal: 'mybranch' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

3 Answers 3

94

As Abizern says, this works:

git push origin mybranch

But, to explain further, the mybranch part is a refspec. This specifies the remote ref which should be updated with the given local commit.

So, the command above is equivalent to:

git push origin mybranch:mybranch

or even:

git push origin mybranch:refs/heads/mybranch

and, indeed, since you're on the local mybranch, you could have done:

git push origin HEAD:mybranch

This is good to understand, because I often find myself doing things like:

git push origin HEAD^:mybranch

where you want to push all but the topmost patch to the remote branch.

Finally, if you want to delete the remote mybranch, you do:

git push origin :mybranch
5
  • 4
    Because the refspec is <src>:<dst>, by leaving off the <src> part, this basically says to make the topic branch on the remote nothing, which deletes it. source - last paragraph Mar 6, 2013 at 21:31
  • 1
    @JeremyIglehart Do you have an updated link? Sounds interesting. Feb 11, 2020 at 22:18
  • @cambunctious It looks like they updated the book and reorganized the chapters. It's now in Chapter 10.5 under "Deleting References" – This also referenced in the git-push documentation under the <refspec> option. Look for "Pushing an empty <src> allows you to delete the <dst> ref from the remote repository." Hope that helps. Feb 12, 2020 at 14:32
  • By the way, Chapter 10.5 is "Git Internals", "The Refspec" – just in case the Chapters get renumbered in the future, so that people can find it. (I didn't get back here soon enough to edit my comment) Feb 12, 2020 at 14:42
  • I was slightly confused by "topic branch". It's not a technical term, but just the name of the branch in the example in the Git book. Thanks for the update. Feb 12, 2020 at 18:01
50

Try

git push origin mybranch

This pushes your branch named mybranch to the remote named origin

0
7

This is an old question, but I used this page as a ref back in the day, and have an answer with a different perspective. From my experience, the best way is to tweak your config settings such that a git push is all that you will have to enter in the end.

You will push to the same remote branch that you update your code from:

  • git config --global push.default upstream

And now, you set the remote branch as upstream (if it wasn't already):

  • git branch --set-upstream-to origin/the_master
  • NOTE: Older versions can fall back upon this deprecated form of the command/.
  • git branch --set-upstream local_branch origin/the_master

You have two branches - a local and a remote. Doing a git pull or git push without args should, and will now, do what you want. This will not limit you to pushing to remote branches with the same name as the local one, as some of the above commands do.

One final thing I usually do: modify git pull to change from this sequence:

  • git fetch && git merge [remote_upstream] to
  • git fetch && git rebase [remote_upstream]

With this command: git config --global branch.autosetuprebase remote

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.