gitgit-push

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.


Solution

  • 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 branch."$branch".remote)
    remote_ref=$(git config --get branch."$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