Last Updated: February 25, 2016
·
448
· klj613

amending previous commits

git commit --amend is simple enough to use... however what if the commit isn't the latest commit?

First you would find out what is the SHA of the commit you want to amend. Then git rebase SHA~1 and then change 'pick' to 'edit'.

At this point you are in a detached HEAD state (your HEAD points to that specific commit, not a branch).

You can make any changes here and then git commit --amend then git rebase --continue.


However in my experience sometimes I need to test the changes I've made however something else is broke which I've fixed in a future commit (after this one).

So I changed my workflow accordingly...

Before I rebase I would make the changes I want, test them and then...

git commit -m "foo"
git rebase SHA~1
move the "foo" commit to the second line and change 'pick' to 'fixup'

Done.

Note

I use both of the above... depending on the situation.