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 ()
(interactive)
(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."
(interactive
(let ((source (magit-read-local-branch-or-commit "Push")))
(list source)))
(magit-git-command-topdir (concat "git push origin " source ":refs/for/master")))
(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.
Commented
Apr 24, 2017 at 22:37
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/…
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.
EDIT
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.
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
.