MiniGod
MiniGod

Reputation: 3802

How to push multiple branches from multiple commits?

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?

Upvotes: 72

Views: 52794

Answers (6)

David V. Corbin
David V. Corbin

Reputation: 397

Don't forget that is you have bending changes (in your working-tree) and switch branches those changes may persist over. Then you do add/commit/push and are surprised.

Upvotes: 0

Kerem
Kerem

Reputation: 11576

Late answer but I am gonna share my solution that worked for me.

Finally my /foo/.git/config file looks like:

[core]
    ...

[remote "dev"]
    url = http://dev.foobar.com/foo.git
    fetch = +refs/heads/*:refs/remotes/dev/*
[remote "pro"]
    url = http://pro.foobar.com/foo.git
    fetch = +refs/heads/*:refs/remotes/pro/*

[remote "all"]
    url = http://dev.foobar.com/foo.git
    url = http://pro.foobar.com/foo.git

[http]
    postBuffer = 524288000

And command;

git push all --all

Credits: Pushing to Multiple Git Repositories Simultaneously

Upvotes: 5

Loic H
Loic H

Reputation: 348

You can do a

 git branch | grep "<pattern>" | xargs git push 

Replace the <pattern> with a regular expression to match your branch names and that’s it.

You can add --set-upstream origin if it is your first commit for this branch

Upvotes: 4

kenorb
kenorb

Reputation: 166359

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.

Upvotes: 102

Zitrax
Zitrax

Reputation: 20256

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 

Upvotes: 113

Eric
Eric

Reputation: 3172

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.

I don't believe there is anything simple or possible to do accidentally that would push commits from two different branches up to the same branch and do the merge on the remote.

I would guess that the new branch had the commits from master in it's history. To see if that is true, run git log my-new-branch locally and see if those commits were in your history.

If so, when you "switched branches" you probably branched off of master after the new commits were made, so the new branch had all of the commits in the history, no just the ones unique to that branch.

Upvotes: 9

Related Questions