Search code examples
gitemacsemacsclient

Correct Way to Exit emacsclient When Set as the Git Editor


I'd like to use emacsclient as my editor in git, i.e. the core.editor setting in my .gitconfig. In my .gitconfig, I have:

...
[core]
  editor = /usr/local/bin/emacsclient --tty
...

When I do something like git commit --amend, or just git commit, I get put into emacs(client) as expected, but when trying to exit with C-x C-c, I get the message:

Don’t kill this buffer #<buffer COMMIT_EDITMSG>.  Instead cancel using C-c C-k

Following the suggestion to cancel above results in an obvious error from git:

error: There was a problem with the editor '/usr/local/bin/emacsclient --tty'.
Please supply the message using either -m or -F option.

What I've discovered is that using C-x # kind of works, my changes are there. However, the next time I run something like git commit --amend, I get the message in emacsclient:

Revert buffer from file /[CONTAINING FOLDER]/.git/COMMIT_EDITMSG? (yes or no)

so I'm guessing this isn't exactly the right way to exit. I've also tried C-x k, but that doesn't seem to save anything.


Solution

  • Looks like the buffer is using (one of the variants of) git-commit mode.

    To accept the commit message use C-c C-c (control+c twice)

    https://github.com/magit/magit/blob/main/lisp/git-commit.el has this commentary:

    ;; By making use of the `with-editor' package this package provides
    ;; both ways of finish an editing session.  In either case the file
    ;; is saved, but Emacseditor's exit code differs.
    ;;
    ;;   C-c C-c  Finish the editing session successfully by returning
    ;;            with exit code 0.  Git then creates the commit using
    ;;            the message it finds in the file.
    ;;
    ;;   C-c C-k  Aborts the edit editing session by returning with exit
    ;;            code 1.  Git then aborts the commit.