Published : 2022-10-23

Edit an old commit

Sometimes, ie. for a review with multiple commits to no squash, it could be useful to edit an old commit in a git branch. First identify the impacted commit to edit (using git log or git blame, for example) then run an interactive rebase on the commit ID. The ^ character is mandatory at the end.

git rebase -i '2abf8fa807^'

In the interactive opened window, on the line with the commit id, replace pick with edit (or e).

Edit your commit then add modified files and finish the rebase:

git add src/filename.cpp src/filename.h # ...
git commit --amend '-S'
git rebase --continue

If modified branch has already been pushed, you will need to run git push --force.