Keep in mind the usual caveats with editing history: don't modify public history and backup your work first (e.g. git branch backup
).
Let's say your working tree is clean and your HEAD is pointing at commit E. Then you can do
git rebase -i A~
You'll get some lines like:
pick E
pick D
...
pick A
Insert a line with 'b' (for break) after pick A
:
pick B
b
pick A
Git will start from the commit before A, apply A and then stop. At this point, you can apply your fixes for A, then git commit --amend
, then git rebase --continue
and resolve merge conflicts as needed. Of course you can also add more b
lines after any other commit that needs fixing at the same time.
I think this is better than the other solution, since the other solution writes the fix commit on the branch's head, which might be too late. e.g. commit A might have an error (or private information, etc.), which commit B removes. Then there's no fix you can write at commit E; the error is already gone. This way you can apply your fix directly on commit A.