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.
HEAD
and edit its commit messageBecause it often happens that you only have to adjust the change or the message, Magit provides two additional variants:
HEAD
without editing the commit messageHEAD
without adding the staged changes to itWhen 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:
A
) the HEAD
.HEAD
(as described above), resulting in commit A'
.A
, but on top of A'
.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.
Modify HEAD
as described above. Then tell Git that you are done by typing r r. If A'
and 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 HEAD
.
Further reading:
git-commit(1)
git-rebase(1)
git-commit
man page redirects to git-rebase(1)
which has these lines: The suggested commit message for the folded commit is the concatenation of the commit messages of the first commit and of those with the "squash" command, but omits the commit messages of commits with the "fixup" command. IOW, use fixup if you just want to fix the code in the previous commit, use squash if you also want to fix the commit message.
Jun 8, 2018 at 12:59
git commit --amend –C HEAD
is the Git command you want to be looking for, and you can do amends in Magit with C-c C-a
.
C-c C-a
is from an older version (I think). Furthermore, I see no trace of "amend" in the help buffer (?
).
May 26, 2016 at 14:57
So one workflow is:
Then
The autosquash will automatically move all !fixup commits to the right place and set them to be squashed on the re-base.
i
yields me Cannot rebase: Your index contains uncommitted changes. Please commit or stash them.
. Except I don't have any uncommitted changes. : /
May 26, 2016 at 15:28
Proceed despite merge in rebase range? [c]ontinue, [s]elect other, [a]bort
. Is it trying to tell me that my fixup might poop on the upcoming merge?
May 26, 2016 at 15:35
merge in rebase
, see BUGS under git help rebase
. I suggest doing the fixup before pulling upstream.
For amending the last commit, it's "c a". Fixup is for amending some older commit.