donquixote
donquixote

Reputation: 5489

git push non-current branch, omit remote name

Let's say I have:

I want to push 'branch-A' to 'remote-A/branch-A' without specifying the remote name, and without git checkout branch-A.
How?

Obviously the following would work:

git push remote-A branch-A

But I really want to omit the 'remote-A' part.

Upvotes: -1

Views: 51

Answers (3)

j6t
j6t

Reputation: 13582

You can set push.default to matching and shorten the remote name:

git remote rename remote-A rmt-A
git remote rename remote-B rmt-B

Now you can just

git push rmt-A    # pushes only branch-A
git push rmt-B    # pushes only branch-B

This assumes that you do not have branch branch-A on remote-B nor branch-B on remote-A.

Upvotes: 1

LeGEC
LeGEC

Reputation: 52081

Technically, the remote name and remote ref name are stored in your local configuration:

$ git config --get branch.branch-A.remote
remote-A
$ git config --get branch.branch-A.merge
refs/heads/that/awful/remote/branch/name

Here is a basic script to get the arguments you want for git push:

#/bin/bash

branch=$1

remote=$(git config --get config."$branch".remote)
remote_ref=$(git config --get config."$branch".merge)

if [ -z "$remote_ref" ]; then
    echo "*** branch '$branch' has no upstream branch defined, cannot push" >&2
    exit 1
fi

echo "# pushing to $remote $remote_ref ..."
git push "$remote" "$branch":"$remote_ref"

If you name this script git-pushx (<- a name starting with git-, and ending with whatever you want), and place it on your PATH, you can then type:

$ git pushx branch-A
$ git pushx branch-B

Upvotes: 2

bluestar
bluestar

Reputation: 100

You can set push.default to upstream like

git config --global push.default upstream

and then, git push branch-A should work.

Upvotes: -1

Related Questions