I have 2 commits, A then B, ready to be pushed. I realize I forgot to add something in A.
How can I add this change to A using Magit? I don't even know which part of the Git documentation I should look at.
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.Sign up to join this community
Let's pretend for a moment that you want to add something to the
HEAD commit, i.e. "the second commit B" in your example.
The commit popup on c features a binding "a Amend". Pressing that key will "amend" the staged changes to the
HEAD commit. Since commits are not mutable in Git, this will actually replace the old commit with a new commit. A buffer with the old commit message will pop up, so that you can modify it in case the added change also requires that you adjust the message. As always, press C-c C-c when you are done editing the message. This is equivalent to running
git commit --amend on the command line.
HEADand edit its commit message
Because it often happens that you only have to adjust the change or the message, Magit provides two additional variants:
HEADwithout editing the commit message
HEADwithout adding the staged changes to it
When you want to edit a commit that isn't
HEAD, then the above won't work. These commands always "modify" (i.e. replace) the
HEAD commit. Git doesn't provide a single command for modifying a commit other than
HEAD so this is a bit more involved.
Magit does provide such a command, but because there are situations in which it is preferable to do this in multiple steps, we will discuss that first.
Modifying a commit other than
HEAD can be broken down into three steps:
HEAD(as described above), resulting in commit
A, but on top of
This can be done using an interactive rebase. Type r to show the rebase popup. Then type m to invoke the "edit a commit" rebase variant. A buffer with recent commits appears. Move to the commit you want to modify and type C-c C-c to select it. Git then rewinds history to that commit and shows information about the ongoing rebase in the status buffer.
HEAD as described above. Then tell Git that you are done by typing r r. If
B conflict then rebase will stop at
B and you have to resolve the conflict. After you have done so press r r to continue.
If you know that your changes to
A will result in conflicts with
B, then proceed as describe above, otherwise use the following approach.
Git allows creating "fixup commits" using
git commit --fixup A. This creates a new commit, which records changes which "should have been made in another commit". That commit becomes the new
HEAD. There also exists a
--squash variant. For information about the differences see the
git-commit man page.
To actually combine the
A commit and the new commit
A' and then reapply
B on top of that you have to use rebase. Magit provides a convenient command for doing so on r f.
The main difference to the above approach is that here we first create a new commit and then we rebase to combine that with the "target" and reapply
B. Above we began with rebasing instead of committing.
In Magit both the
--fixup and the
--squash variants are available from the commit popup, on f and s. But Magit also provides "instant" variants of the fixup and squash commands on F and S. These variants create a new commit like the "non-instant" variants, but then they instantly combine the fixup commit with the target commit using rebase, without you having to invoke another command.
"Instant fixup" (c F) is essentially the same thing as "extend
HEAD" (c e), except that it works for any commit, not just
git commit --amend –C HEAD is the Git command you want to be looking for, and you can do amends in Magit with
So one workflow is:
The autosquash will automatically move all !fixup commits to the right place and set them to be squashed on the re-base.
For ammending the last commit, it's "c a". Fixup is for ammending some older commit.