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?

2 Answers 2


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:

(transient-append-suffix 'magit-push "p"
  '("m" "Push to gerrit" magit-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")))
  • 3
    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
    Commented Apr 24, 2017 at 22:37
  • From magit-obsolete.el: * If you add additional arguments and/or actions to Magit's popups, then you have to port that to modify the new \"transients\" instead. See github.com/magit/magit/wiki/…
    – user30747
    Commented Oct 15, 2019 at 18:07

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? :) Commented Jan 20, 2016 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
    Commented Jan 21, 2016 at 10:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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