Sublime Forum

Push current commit to another remote branch

#1

Is it possible to push the current commit to a different custom remote branch? I am asking for the equivalent of this exact command:

git push origin HEAD:refs/for/master

This is very useful when working with Gerrit for example: https://gerrit-review.googlesource.com/Documentation/user-upload.html#push_create . This command is used every time you want to push a new commit or fix an existing one.

Note: The refs/for/master is not a branch you can checkout; it’s a “magical” (like Gerrit docs says) recipient to send new commits to.

1 Like

#2

I would like this functionality too, but can’t figure out whether it’s possible. My use case is for remote deployment branches - I push from a local branch to a differently-named remote branch that triggers a deployment, so there’s no need to keep local copies of those branches and manually merge before pushing.

0 Likes

#3

There is a special Merge command to allow executing an arbitrary command. It won’t necessarily set certain env variables we set to make sure the credential helper is available, but it should do for now.

You’ll want to create a file named Packages/User/Default.sublime-commands and place the following in it:

[
    {
        "caption": "Push refs/for/master",
        "command": "git",
        "args": {
            "argv": ["push", "--progress", "origin", "HEAD:refs/for/master"]
        }
    }
]

Once this file is there, the command “Push refs/for/master” should be present in your command palette.

4 Likes

#4

Is it possible to create such a command that uses the selected commit rather than HEAD? (already using the HEAD version, but made me wonder :slight_smile:)

0 Likes

#5

Try replacing the above instance of “HEAD” with “$commit”.

This will be populated at run-time with the selected commit ID. Hope this helps!

- Dylan

2 Likes

#6

Is it possible to create command for different branches? e.g. for HEAD:refs/for/branch1 and for HEAD:refs/for/branch2 and for master? May be command with param branch_name, if its possible? How I can create command with param entered from the keyboard?

1 Like

#7

Yes. If you add something like this to Packages/User/Default.sublime-commands you’ll get two push commands, one which pushes to origin/production and the other which pushes to origin/staging. When you bring up the command palette you can start typing production or staging and you’ll get those commands coming up.

[
    {
        "caption": "Push production",
        "command": "git",
        "args": {
            "argv": ["push", "--progress", "origin", "HEAD:production"]
        }
    },
    {
        "caption": "Push staging",
        "command": "git",
        "args": {
            "argv": ["push", "--progress", "origin", "HEAD:staging"]
        }
    }
]
1 Like

#8

Try replacing the above instance of “HEAD” with “$commit”.

Thanks for this very valueable information, this enables using custom commands to do interactive rebasing, creating commit fixups and whatnot.
It took me a while to find this though, are there other variables like that, and is this documented somewhere? Likewise for keybindings etc: I found all (?) commands by unzipping the .package file but it would be nice to have this listed in a more accessible way.

0 Likes

How to push a branch to a different remote branch
#9

A bit late to this (hopefully not too much so) but is there a similar variable that contains the current branch name? Someone already asked this, but there didn’t seem to be an answer… is there somewhere we could find what current variables are available?

0 Likes

#10

Available variables can be found here. The variables available to all menus are also available to commands. As far as I can tell, additional variables for specific menus are not available to commands.

If the branch name is required within a command, the only workaround I know of is to look it up within a function-based git alias. examples: 1, 2

1 Like

#11

Thank You!

0 Likes

#12

I added a custom command in Packages/User/Default.sublime-commands to push to Gerrit. But I haven’t found how to specify the remote branch. The answer to use variable inspired me. I found an alternative way to specify branch name by select branch in the Location Bar. So I can add a context menu entry in Packages/User/Branch.sublime-menu to ref that branch with $branch

[
    {
        "caption": "Push HEAD to refs/for ...",
        "command": "git",
        "args": {
            "argv": ["push", "--progress", "origin", "HEAD:refs/for/$branch"]
        }
    }
]
0 Likes

#13

Would you, please, share with code snippet for the branch.sublime-menu how you refer the selected branch to the $branch? Regards!

0 Likes