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.
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.
- a Amend — add the staged changes to
HEAD and edit its commit message
Because it often happens that you only have to adjust the change or the message, Magit provides two additional variants:
- e Extend — add the staged changes to
HEAD without editing the commit message
- w Reword— change the message of
HEAD without 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:
- Temporarily make that other commit (
- Modify the
HEAD (as described above), resulting in commit
- Tell Git to reapply the commits that followed
A, but on top of
This can be done using an interactive rebase. Type r to show the rebase popup. Then type mto 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
Bconflict 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 fand 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