Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to push multiple branches from multiple commits?

Tags:

git

github

I never used git before GitHub released the Windows app, so I've never used it in command line.

So here's my situation:
I did some commits on master, then switched branch and did some commits there too. All without pushing to GitHub. When I then clicked sync in the the windows app (which I assume does git push), to my surprise, all my commits were pushed to my new branch - even the commits I made while I was in master.

Since this is the behavior of the windows app, I guess I have to use the command line.
What is the correct git push command to push the commits to the correct branches on the remote?

like image 591
MiniGod Avatar asked Oct 21 '12 17:10

MiniGod


People also ask

How do you push a commit to multiple branches?

Short answer. You can apply already existing commit to another branch using cherry-pick command, and then push both branches using git push origin branchA branchB .

How do I push multiple branches to a remote?

git push origin will push from all tracking branches up to the remote by default. git push origin my-new-branch will push just your new branch.

Can we do multiple commits before pushing?

Committing takes place only within your repository; it has nothing to do with whether or not you're online. The things that you need to be online for are pushing (publishing your commits to another repository) and pulling (fetching and merging commits from another repository).

Does git push affect all branches?

No, git push only pushes commits from current local branch to remote branch that you specified in command.


2 Answers

To push all branches (refs under refs/heads), use the following command (where origin is your remote):

git push origin --all 

You can also set push.default to matching in your config to push all branches having the same name on both ends by default. For example:

git config --global push.default matching 

Since Git 2.0 the default is simple which is the the safest option.

like image 149
kenorb Avatar answered Oct 02 '22 11:10

kenorb


If you want to push several specific branches (for example branch1 and branch2) you can use:

git push origin branch1 branch2  

In Git >= 2.4 this operation can be done atomically (i.e. if it fails to push any of the branches specified nothing will be pushed):

git push --atomic origin branch1 branch2  
like image 20
Zitrax Avatar answered Oct 02 '22 11:10

Zitrax