Alexey Kamenskiy
Alexey Kamenskiy

Reputation: 2948

git push NOT current branch to remote

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.

Upvotes: 41

Views: 13538

Answers (3)

Tim
Tim

Reputation: 1045

When I tried to do push a, for the remote, new branch using --set-upstream (-u)

git push MyLocalBranch -u origin MyLocalBranch

and I did not have the branch checkout out, I received the error

fatal: 'MyLocalBranch' does not appear to be a git repository
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Thanks to the comment git push NOT current branch to remote

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.

from Lars Noschinski I figured that the solution was to call

git push -u origin MyLocalBranch:MyLocalBranch

Upvotes: -1

RayLuo
RayLuo

Reputation: 19200

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.)

Upvotes: 53

Gabriele Petronella
Gabriele Petronella

Reputation: 108101

With git push you can specify the remote and the local

git push remotename branchname

Upvotes: 37

Related Questions