11

This StackOverflow question has the best solutions/approaches to undo last commits.

I started using Magit a couple a weeks ago and after looking through all its documentation I still don't have a definitive/best way to undo a mistakenly committed change.

For example:
For undo and redo the last commit we can use:

$ git commit -m "Something terribly misguided"
$ git reset HEAD~
<< edit files as necessary >>
$ git add ... 
$ git commit -c ORIG_HEAD # To keep the same commit message

What should be the best workflow to reproduce this procedure using Magit?

15

You can use magit-reset (bound by default to x). The commit at point will be used as the default for the prompt, but you can enter any revision value you'd like ("HEAD~" here).

The resetting commands are described in the "Resetting" section of the manual: https://magit.vc/manual/magit/Resetting.html

To reuse the commit message, you can use the =C option in the commit popup. Unforuntately, the prompt doesn't currently offer any completion values (I think it should probably at least offer ORIG_HEAD), but before resetting you can put the hash of the current commit in the kill ring (e.g., with C-w on the "Head:" line of the status buffer) and then, after resetting, yank it as the value for the =C option.


Some additional comments:

  • Instead of using =C, you may find it more convient to start off the commit (cc) and then use git-commit-prev-message (M-p) to cycle through previous messages.

  • If you are only modifying the last commit, you can tack on staged changes to the previous commit with magit-commit-amend (ca) or magit-commit-extend (ce). (The latter reuses the commit message, bypassing the COMMIT_EDITMSG buffer.)

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.