1

After I open a Magit status buffer and make a commit, I commonly also want to copy that commit's hash (the shortened version) to my clipboard to post somewhere else.

I can't work out how this should be done, and the Magit documentation doesn't seem to describe this feature. This makes me suspect there's a general way one should do this on these sorts of buffers that I don't yet know about.

I am using Doom Emacs, so I get to the Magit buffer via SPC g g.

4

After I open a Magit status buffer and make a commit, I commonly also want to copy that commit's hash

With vanilla Emacs bindings in the Magit status buffer:

M-w (magit-copy-buffer-revision) saves the latest commit hash to the kill-ring:

M-w runs the command magit-copy-buffer-revision (found in
magit-status-mode-map), which is an interactive compiled Lisp function in
`magit-extras.el'.

It is bound to M-w.

(magit-copy-buffer-revision)

Save the revision of the current buffer for later use.

Save the revision shown in the current buffer to the `kill-ring'
and push it to the `magit-revision-stack'.

This command is mainly intended for use in `magit-revision-mode'
buffers, the only buffers where it is always unambiguous exactly
which revision should be saved.

Most other Magit buffers usually show more than one revision, in
some way or another, so this command has to select one of them,
and that choice might not always be the one you think would have
been the best pick.

In such buffers it is often more useful to save the value of
the current section instead, using `magit-copy-section-value'.

When the region is active, then save that to the `kill-ring',
like `kill-ring-save' would, instead of behaving as described
above.

When `magit-copy-revision-abbreviated' is non-nil, save the
abbreviated revision to the `kill-ring' and the
`magit-revision-stack'.

C-w (magit-copy-section-value) saves the commit hash at point to the kill-ring:

C-w runs the command magit-copy-section-value (found in magit-status-mode-map),
which is an interactive compiled Lisp function in `magit-extras.el'.

It is bound to C-w.

(magit-copy-section-value ARG)

Save the value of the current section for later use.

Save the section value to the `kill-ring', and, provided that
the current section is a commit, branch, or tag section, push
the (referenced) revision to the `magit-revision-stack' for use
with `magit-pop-revision-stack'.

When `magit-copy-revision-abbreviated' is non-nil, save the
abbreviated revision to the `kill-ring' and the
`magit-revision-stack'.

When the current section is a branch or a tag, and a prefix
argument is used, then save the revision at its tip to the
`kill-ring' instead of the reference name.

When the region is active, then save that to the `kill-ring',
like `kill-ring-save' would, instead of behaving as described
above.  If a prefix argument is used and the region is within
a hunk, then strip the diff marker column and keep only either
the added or removed lines, depending on the sign of the prefix
argument.

(the shortened version)

The user option magit-copy-revision-abbreviated controls whether the hash is shortened:

magit-copy-revision-abbreviated is a variable defined in `magit-extras.el'.

Its value is nil

  You can customize this variable.
  This variable was introduced, or its default value was changed, in
  version 3.0.0 of the magit package.

Whether to save abbreviated revision to `kill-ring' and `magit-revision-stack'.

the Magit documentation doesn't seem to describe this feature. This makes me suspect there's a general way one should do this on these sorts of buffers that I don't yet know.

These commands are documented under (info "(magit) Common Commands"):

‘C-w’     (‘magit-copy-section-value’)

     This command saves the value of the current section to the
     ‘kill-ring’, and, provided that the current section is a commit,
     branch, or tag section, it also pushes the (referenced) revision to
     the ‘magit-revision-stack’.

     When the current section is a branch or a tag, and a prefix
     argument is used, then it saves the revision at its tip to the
     ‘kill-ring’ instead of the reference name.

     When the region is active, this command saves that to the
     ‘kill-ring’, like ‘kill-ring-save’ would, instead of behaving as
     described above.  If a prefix argument is used and the region is
     within a hunk, then it strips the diff marker column and keeps only
     either the added or removed lines, depending on the sign of the
     prefix argument.

‘M-w’     (‘magit-copy-buffer-revision’)

     This command saves the revision being displayed in the current
     buffer to the ‘kill-ring’ and also pushes it to the
     ‘magit-revision-stack’.  It is mainly intended for use in
     ‘magit-revision-mode’ buffers, the only buffers where it is always
     unambiguous exactly which revision should be saved.

     Most other Magit buffers usually show more than one revision, in
     some way or another, so this command has to select one of them, and
     that choice might not always be the one you think would have been
     the best pick.

   Outside of Magit ‘M-w’ and ‘C-w’ are usually bound to
‘kill-ring-save’ and ‘kill-region’, and these commands would also be
useful in Magit buffers.  Therefore when the region is active, then both
of these commands behave like ‘kill-ring-save’ instead of as described
above.
2
  • I was struggling with what turned out to be an ancient version of magit: this was just the kick in the behind I needed to upgrade and start using it (I've been a command-line user of git till now). So thanks for the question and the answer! – NickD Apr 20 at 21:57
  • Ah, I hadn't understood that the "kill-ring" is the copy buffer I was looking for. Thanks! – SLesslyTall May 3 at 18:55

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.