35

Is there a way in git bare repository to push a branch that is not in HEAD right now?

For example i have two branches:

$ git branch
* master
  another

And i have two remotes set: origin and another.

I need to be able push from another to another/another just in one command without changing HEAD.

Alexey Kamenskiy
  • 2,720
  • 3
  • 34
  • 55

2 Answers2

42

All those "another another" in the original question, the answer and lots of comments are so confusing (which is a perfect example of why it is important to name your things right in the first place), I can't help helping (pun not intended) to write yet another answer as below.

Q: Is there a way in git (bare) repository to push a branch that is not in HEAD right now? For example i have two branches and two remotes. I need to be able push from feature to upstream/feature just in one command without changing HEAD.

$ git branch
* master
  feature
$ git remote
origin
upstream

A: Do git push remote_name branch_name. In the case above, it looks like this.

$ git push upstream feature

Q: Does it mean that it will push local feature to upstream/feature? I always thought it will push current HEAD to upstream/feature.

A: Yes. The feature part is a refspec, which has the form src:dst. This means to push the local branch src to the remote branch dst. If :dst is omitted, the local branch src is pushed to the remote branch src. You can specify a different name as remote branch too. Just do:

$ git push upstream feature:cool_new_feature

(Thanks @gabriele-petronella and @alexkey for providing materials for this answer.)

RayLuo
  • 14,800
  • 4
  • 80
  • 70
  • Note that "upstream" is just an alias for a particular remote. Odds are, if you've been following the most typical convention online (ie, you copy paste a lot), it's actually gonna be "origin". See: https://stackoverflow.com/questions/9529497/what-is-origin-in-git – Kat Nov 30 '17 at 17:12
  • 1
    @Kat: while both `upstream` and `origin` are "just" an alias for a particular remote, they have different meanings, by convention of [github fork workflow](https://help.github.com/articles/configuring-a-remote-for-a-fork/). Do not assume you can always replace `upstream` by `origin` in any git command, unless you know what you are doing. – RayLuo Nov 30 '17 at 19:56
33

With git push you can specify the remote and the local

git push remotename branchname
Áxel Costas Pena
  • 5,295
  • 5
  • 27
  • 58
Gabriele Petronella
  • 104,800
  • 21
  • 210
  • 229
  • Does it mean that it will push local another to another/another? I always thought it will push `current HEAD` to another/another. – Alexey Kamenskiy Mar 29 '13 at 08:21
  • 4
    Yes. (The second) `another` is a refspec, which (in general) has the form `src:dst`. This means to push the local branch `src` to the remote branch `dst`. If `:dst` is omitted, the local branch `src` is pushed to the remote branch `src`. – Lars Noschinski Mar 29 '13 at 08:28
  • 1
    the current HEAD is just the default, but you can specify any branch (o more generally any refspec) as Lars was pointing out – Gabriele Petronella Mar 29 '13 at 08:29
  • 1
    @LarsNoschinski So technically I can even specify to push local another to remote/master by doing `$ git push another another:master`? (of course that's not what i am going to do, but just want to make sure i understand it right). – Alexey Kamenskiy Mar 29 '13 at 08:34
  • Yes your understanding is correct. That would push the content of the local `another` to the remote `another/master` – Gabriele Petronella Mar 29 '13 at 08:35
  • 3
    As a matter of fact `git push another another` is totally equivalent to `git push another another:another`. I feel like I cannot stand another `another`, though. – Gabriele Petronella Mar 29 '13 at 08:37
  • 2
    Didn't work for me, but that's because git was written by people who hate documentation. – cbmanica Jun 20 '14 at 16:51
  • 1
    I found this explanation was misleading. As of git 2.14.1, I had to do `git push origin mybranch:mybranch`. – Mike Funk Sep 08 '17 at 19:06
  • 1
    This comment thread has gotten out of hand. Is the only way to do it now `git push origin otherbranch:otherbranch` BTW it's 2019 now. – Greg K. Apr 11 '19 at 22:59