Amend the second to last commit
@aragaer 's comment is even shorter way of doing the same:
git commit -a --fixup=HEAD^ #(or whatever commit to be fixed)
then
git rebase -i HEAD~3
and you don't need to change anything, so you can just close editor and git
will handle the fixup itself.
Note: This doesn't work on my Ubuntu system!
Well, I landed on this page while searching for the same. Found a better way with many other options
git rebase -i HEAD~2
An editor will open up with the following details
pick 4f4f96f Added git ignore
pick d01e18c Added sample Blog
# Rebase 60e1cd3..d01e18c onto 60e1cd3 (2 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
press i, change pick
to r
or reword
, something like below
pick 4f4f96f Added git ignore
r d01e18c Added sample Blog
PRESS esc + : + wq
Another window will open up, change the commit message
PRESS esc + : + wq
git push -f
commit your fixup with
git commit -a -m "fix commit (this one will be shifted up one line)"
(the commit message is not important, it will be obsolete once you are finished)
Now the magic:
rebase the HEAD to the second last commit but edit the commit message before in that way, that you swap the last and the last but one line in the message editor:git rebase -i HEAD~3
The editor will show the last 3 comits like this:
pick 2a06f16 this was the last commit pick 0dbc5ce the last-but-one commit pick 2e30418 fix commit (this one will be shifted up one line) # Rebase 011f3d0..2e30418 onto 011f3d0 # …
swap the last two lines so the commit message will look for example like this:
pick 2a06f16 this was the last commit fixup 2e30418 fix commit (this one will be shifted up one line) pick 0dbc5ce the last-but-one commit # Rebase 011f3d0..2e30418 onto 011f3d0 # …
Check your log with
git log HEAD~3
push the corrected commit-line with
+
to force a new push to the already existing branch withgit push origin +branchname