How can I push a local Git branch to a remote with a different name easily?

I've been wondering if there's an easy way to push and pull a local branch with a remote branch with a different name without always specifying both names.

For example:

$ git clone myrepo.git
$ git checkout -b newb
$ ...
$ git commit -m "Some change"
$ git push origin newb:remote_branch_name

Now if someone updates remote_branch_name, I can:

$ git pull

And everything is merged / fast-forwarded. However, if I make changes in my local "newb", I can't:

$ git push

Instead, I have to:

% git push origin newb:remote_branch_name

Seems a little silly. If git-pull uses git-config branch.newb.merge to determine where to pull from, why couldn't git-push have a similar config option? Is there a nice shortcut for this or should I just continue the long way?


When you do the initial push add the -u parameter:

git push -u origin my_branch:remote_branch

Subsequent pushes will go where you want.

EDIT:

As per the comment, that only sets up pull.

git branch --set-upstream

should do it.


Sure. Just set your push.default to upstream to push branches to their upstreams (which is the same that pull will pull from, defined by branch.newb.merge), rather than pushing branches to ones matching in name (which is the default setting for push.default, matching).

git config push.default upstream

Note that this used to be called tracking not upstream before Git 1.7.4.2, so if you're using an older version of Git, use tracking instead. The push.default option was added in Git 1.6.4, so if you're on an older version than that, you won't have this option at all and will need to explicitly specify the branch to push to.


The command by Adam is now deprecated. You can use:

git branch --set-upstream-to origin/my_remote_branch my_local_branch

to set the upstream branch of my_local_branch to origin/my_remote_branch.


I have been running into the same issue for quite sometime now. I finally have a set of statements so I don't have to do git push origin local:remote every time. I followed these:

git branch --set-upstream-to origin/remote_branch_name
git config push.default upstream
git push

After setting upstream to a remote branch with different name (1st line) and then making that upstream as default (2nd line), 3rd line will now obey these rules and push to the set upstream.


Push and create a temporary remote branch

If you want to:

  • Push the current branch to remote under a new name, but:
  • Don't change the remote tracking branch of current branch, and:
  • Don't create a local branch under the new name,

Then it's as simple as this:

git push origin HEAD:temp-branch-name

Note: You can replace HEAD with any other branch or commit ID to push that instead.