I am using gerrit. When I want to push my commits, I use following command:

git push origin HEAD:refs/for/master

How can I recreate the same command using Magit?


After a while I managed to have enough time to try to fix it myself.

I don't know of any "out-of-the-box" way to do this; however, you can expand magit to fit your needs.

Firstly, I wrote a function that would push to gerrit:

(defun magit-push-to-gerrit ()
  (magit-git-command "push origin HEAD:refs/for/master" (magit-toplevel)))

Next, I added it to push menu:

(magit-define-popup-action 'magit-push-popup
  "Push to gerrit"

These two forms are in my init file.

And that's it! Now I can use P m to push to gerrit.

Update: If you want to push a specific commit and not HEAD, you can use this function (also updated with magit-git-command-topdir as referred to in the comment):

(defun magit-push-to-gerrit (source)
  "Push an arbitrary branch or commit to gerrit. The source is read in the minibuffer."
   (let ((source (magit-read-local-branch-or-commit "Push")))
     (list source)))
  (magit-git-command-topdir (concat "git push origin " source ":refs/for/master")))
  • 1
    If using an up-to-date Magit, you'll get an error from running (magit-git-command "push origin HEAD:refs/for/master" (magit-toplevel)). You'll want to use (magit-git-command-topdir "git push origin HEAD:refs/for/master") instead. – user30747 Apr 24 '17 at 22:37

Use ! to use a generic git command, then you can simple write: push origin HEAD:refs/for/master almost as usual.

There is also a dedicated package for gerrit here: https://github.com/terranpro/magit-gerrit I haven't used it personally however, so don't know exactly how to use it.


In a recent version of magit, it is now possible to specify an explicit git refspec from the r subcommand. Thus, to push to the gerrit refspec, simply use:

P r "origin" RET "HEAD:refs/for/master"

Although, this is not really a shortcut since you have to do even more key-presses, but it is available out of the box.

  • How about some shortcuts? :) – MatthewRock Jan 20 '16 at 22:41
  • Afraid I don't know any specific shortcuts. I did just try a regular git push 'elsewhere' but that prepends 'refs/heads/' to the branch destination, so that doesn't seem to work out of the box for gerrit. – Xaldew Jan 21 '16 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.