1

Lets say we have origin with a branch called master which would be checked out by;

>> git checkout master
Switched to branch 'master'
Your branch is up-to-date with 'origin/master'

Now I add the remote fork also with a branch called master which would be checked out by;

>> git checkout -b fork-master fork/master
Switched to branch 'fork-master'
Your branch is up-to-date with 'fork/master'

This clearly knows what remote a given branch belongs to, and it even references the correct origin.

Lets say I want to push to origin I'd do this;

>> git push

Which would update origin/master with all changes (duh).

Below are 2 examples of pushes;

Example 1 **Correct!**;
>> git push fork fork-master:master

Example 2 **Fails**;
>> git push fork --all

This automatically pushes all fork branches to origin except for branch master which will be rejected (in case changes where made).

What I thought example #2 would do is to push fork-master to fork/master, but this isn't happening.

Can I push all remote specific branches at once without having to point them to the correct remote branch name?

I'm asking because there are a lot of branches from time to time to push.

Daniel
  • 3,726
  • 4
  • 26
  • 49
  • I think you would have to configure [`push.default=upstream`](http://git-scm.com/docs/git-config) and then set up `fork-master` to track `fork/master` – knittl Jun 11 '15 at 05:37
  • @knittl Yeah that was what I thought too but it still pushes all origin masters to `fork`. – Daniel Jun 11 '15 at 07:27
  • What do you mean with »all origin masters to `fork`«? You have one local branch `fork-master` which is setup to track `fork/master` (verify with `git branch -vv`!) Now, `git push fork` should only push this single branch. Do not use the `--all` switch in your case. – knittl Jun 11 '15 at 07:39
  • @knittl `git push` would push **all** origin branches. This is what I want to do for other remotes too. I.e `git push fork` (I didn't say this is the right way). If I have 5 branches locally from `fork` I want to be able to push them without having to point them to the correct remote branch. My question is if this is possible. – Daniel Jun 11 '15 at 07:42
  • @knittl Better example; `git push fork fork-master:master fork-updates:master-updates fork-test:master-test fork-123:master-123` How do I push them all without having to point them, if it is possible? – Daniel Jun 11 '15 at 07:43
  • 1
    Set their upstream (verify with `git branch -vv`) and use `git push fork fork-master fork-updates fork-test fork-123`. I'm afraid there is no way to push all branches which have one remote set as their upstream. – knittl Jun 11 '15 at 07:54
  • @knittl Ok, thank you. – Daniel Jun 11 '15 at 07:55
  • Does this answer your question? [How can I push a local Git branch to a remote with a different name easily?](https://stackoverflow.com/questions/5738797/how-can-i-push-a-local-git-branch-to-a-remote-with-a-different-name-easily) – Tom Hale Jun 30 '23 at 14:12

1 Answers1

2

Configure default.push to upstream

git config default.push upstream

Then, setup your local branches to track the respective remote branch:

git checkout --track -b fork-master fork/master

Use the git branch command to verify each branch has its current upstream:

git branch -vv

You can then use git push <remote> <branch> to push each branch to branch@{upstream} automatically:

git push fork fork-master fork-test fork-updates fork-123

I'm afraid there is no way to tell Git to push all branches which have their upstream set to a branch of a specific remote.

But better be specific anyway by passing all branches you want pushed on the command line. Lots of config options change behavior of push, so better be on the safe side.

knittl
  • 246,190
  • 53
  • 318
  • 364