Use Soft Resets to Remove Ammends from a Commit
Posted 2021-04-13
tl;dr use
git reset --soft
to undo amends on a commit.
Sometimes when I’m working on a particularly complicated feature I’ll amend onto my current commit in little working chunks. I generally recommend this practice, but I’ll occasionally run into an issue where I amended a commit with something that’s broken. When I want to cut off the most recent commit and start over. I’ll just git reset HEAD~1
, but in a case like this there’s a lot of work in the current commit I’d like to keep.
In cases like this, you can make use of reflog
to see a more detailed history of you commits. git reflog
will show you something like this:
44e56e8 HEAD@{1}: commit (amend): Move all dialogs to same package
d62a3e2 HEAD@{2}: commit (amend): Move all dialogs
90654b0 HEAD@{6}: commit: Move all dialogs to same package
d949095 HEAD@{7}: commit: Remove unused dialogs
e8cac49 HEAD@{8}: commit: Implement new suprise dialog
e9b67e2 HEAD@{9}: commit (initial): Refactor dialogs to share a base component
The last two “commits” are both amends, adding onto the 90654b0
.
Now, let’s say I wanted to chop off my most recent amend. To do this, I can do a soft reset like so:
git reset --soft @{1}
Now doing a git status
you should see your most recent changes upstaging and ready for modifying.
The full details of a soft reset can be found here.