1

If I accidentally staged a hunk that I wanted a better look at, how do I undo that action (the most recent action). C-/ does not work.

Sure I can U to unstage all, but say all else staged is valid.

4
  • 1
  • Did you try selecting the hunk and then doing u? That works for me.
    – Melioratus
    May 19, 2022 at 16:28
  • The thing is, when this happens and there are many hunks, it get's lost in the stage.
    – mcp
    May 20, 2022 at 20:32
  • The take-away from this is to take greater care when staging changes for any commit. If you have multiple changes in progress, don't just "stage all" (or don't stage all changes for a file). Ultimately, whether you're staging each change piecemeal, or "staging all changes" and then unstaging certain changes, you need to confirm each hunk of the commit. The bottom line is: Always review your commits.
    – phils
    Dec 15, 2023 at 8:34

2 Answers 2

1

In the list of staged changes, move to the accidentally staged change and press u (which unstaged the hunk or file at point, just like s stages the change at point).

There is no undo command which would revert the last change. You do have to locate the accidentally staged change manually.

-1

Solution 1,

M-x magit-unstage

Solution 2:

Any commit/patch/diff could be opened in diff-mode. Then M-x diff-apply-hunk to apply hunk. C-u M-x diff-apply-hunk to undo hunk.

Actually, you don't need switch to diff-mode from magit-revision-mode at all, simple diff-apply-hunk is enough.

Extra tip:

If you use smex or counsel-M-x from counsel, when you type M-x some-keyword, all related commands are displayed.

enter image description here

As you can see, there are other commands to upstage/undo a hunk.

2
  • magit-unstage does not automatically undo the last staged hunk.
    – mcp
    Jan 16, 2022 at 19:13
  • No, there's no "undo" (to my knowledge); you must manually unstage it.
    – phils
    Jan 17, 2022 at 4:51

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.