5

Let's say I just did a commit with changes to two files. Now I would like to have just one of the files in that commit, and I would like to stash the other file. So, in essence, I would like to soft reset, unstage one of the files, recommit with the same message (or maybe change it). And after all that, I will have the second file not committed, and I can stash it, or stage it and commit it anew or whatever.

I used to do that in GitX easily, by just clicking the amend last commit checkbox. It would show the staged changes, the commit message etc, and I could just change the message, or stage more, unstage some, etc.

Is there an easy way to do that in magit?

8

I will have the second file not committed, and I can stash it, or stage it and commit it anew or whatever.

The quickest way to get this in magit is hit RET on the top line of the status to show the HEAD commit, then in the *magit-revision buffer hit u on the part you don't want in the commit (this can be a whole file, a hunk, or even part of a hunk; and you can do this multiple times). Then c e (or c a if you want to edit the commit message) to remove the part(s) from the commit. This requires magit-unstage-committed to be set to t (the default) to allow magit-unstage to fallback to magit-reverse-in-index on committed hunks.


You can also literally follow the sequence you outlined in magit:

I would like to soft reset,

X s HEAD~1 RET (If point is on the commit you want to reset to, then it's the default input, and you don't need to type it).

unstage one of the files,

Navigate to the unstaged file (j u jumps to unstaged section), then u to unstage it.

recommit with the same message (or maybe change it).

c c then M-p to retrieve previous message when the COMMIT_EITMSG buffer pops up.

  • 1
    Very detailed answer. Thank you. The second part is what I am trying to avoid, but I didn't know about M-p. The first part is very close to what I wanted. I will probably make a function out of it, that does the slow parts without me typing, like the going-to-revision-of-last-commit part. Other than that, cool stuff :) – pvinis Jul 19 '16 at 12:39
  • So the going-to-revision-of-last-commit, I can do by hitting RET on the Head: line on the top of the main magit buffer. Thanks to C-Keen from the emacs irc channel for that. – pvinis Jul 19 '16 at 12:53

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.