pavelp

How to discard hunk in vc-mode (Emacs)

On <2025-09-21 Sun>, after upgrade, magit got broken on discarding a hunk:

magit-apply-region: Wrong type argument: 
(or eieio-object cl-structure-object oclosure),
(#s(magit-hunk-section hunk magit-hunk-section-map (nil (8 10) (8 34)) 
#<marker (moves after insertion) at 1708 in magit: Notes>
#<marker at 1726 in magit: Notes> 
#<marker at 2833 in magit: Notes> nil highlight nil ...))

I needed to quickly bring back some deleted lines, so I decided to give built-in vc-mode a try.

{M-x vc-dir} prompts for a directory, defaulting to current working one. The command opens *vc-dir* buffer:

VC backend : Git
Working dir: ~/Notes/
Branch     : main
Stash      : Hide all stashes (2)
             {0}: On main: add puni
             {1}: On main: failure when loading

                         ./
     edited              Pavels-Emacs-Configuration-v3.org
     edited              emacs.org
     edited              llamafile.org
                         src/
     edited              src/pavels-emacs-configuration-v3.el

{C-h m} explains the active keymap, commands are self-explanatory (as I thought). So far so good, point on the file in question, then {M-x vc-diff}, or = or {C-x v =} opens *vc-diff* buffer.

There interesting started, so I had to scratch my beard. Some relevant commands, like {M-k} (diff-hunk-kill) did kill the hunk, but not changed the underlying file.

After consulting Claude, it provided correct answer, but it was counter-intuitive how to actually use it, so I had to experiment. And here's the recipe:

  • {C-x v =} to open a diff for a file.
  • Select lines, {C-c C-s} to split selected lines into separate hunk.
  • {C-c C-a} to "apply the hunk", it will be effectively be undone in the original file, but it will stay in the diff itself.
  • Return back to the file, the hunk will be gone, but the buffer will not be saved yet.
  • Save the buffer.
  • Optionally {g} in the diff buffer to see the hunk gone.

Googling brought up an interesting blog post, keeping here for posterity:

Phew, waiting for the fix of regression in Magit.

Thoughts? Leave a comment