Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git push NOT current branch to remote

Tags:

git

git-push

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.

like image 232
Alexey Kamenskiy Avatar asked Mar 29 '13 08:03

Alexey Kamenskiy


People also ask

How do you push to a remote branch that does not exist?

Create a new branch with git checkout -b <branch_name> , and push it with git push -u origin <branch_name> . Then, everyone can see your branch, and if you need to make changes to it, it's as straightforward as changing to the branch, committing, and pushing it up.

Does git push only push the current branch?

By default, git push only updates the corresponding branch on the remote. So, if you are checked out to the main branch when you execute git push , then only the main branch will be updated. It's always a good idea to use git status to see what branch you are on before pushing to the remote.

How do I push to a different remote branch?

In order to push your branch to another remote branch, use the “git push” command and specify the remote name, the name of your local branch as the name of the remote branch.

Why git push is not working?

If git push origin master not working , all you need to do is edit that file with your favourite editor and change the URL = setting to your new location. Assuming the new repository is correctly set up and you have your URL right, you'll easily be able to push and pull to and from your new remote location.


2 Answers

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

like image 185
RayLuo Avatar answered Oct 07 '22 05:10

RayLuo


With git push you can specify the remote and the local

git push remotename branchname 
like image 41
Gabriele Petronella Avatar answered Oct 07 '22 06:10

Gabriele Petronella