You made a bad git commit but haven't pushed it yet — how do you go back to the previous commit and keep editing? Since I'm basically an idiot who has to look this up every single time, I had AI throw together the simplest possible guide.1. Undo the last commit but keep the changes (my case) The most common need: you committed by mistake, you want to go back to the previous commit, keep working, then commit again. git reset --soft HEAD~1 # undo the commit, changes go back to the staging area or git reset HEAD~1 # undo the commit, changes go back to the working tree (unstaged) HEAD~1 = the previous commit (the parent)then just edit your files → git add → git commit to commit againThree reset modes, and this one table is all you need to remember: Command HEAD Staging area Working tree files Effect git reset --soft HEAD~1 moves back kept kept undoes the commit, changes stay staged git reset HEAD~1 (default, mixed) moves back reset kept undoes the commit, changes become unstaged git reset --hard HEAD~1 moves back reset wiped undoes the commit and throws the changes away ▎ --hard deletes your changes, so be careful. If you want to "go back to the previous commit and keep editing", always use --soft or the default mixed.2. You just want to add something to the last commit No need to undo and redo — just fold the new changes in:Copygit add <the files you changed> git commit --amend --no-edit # fold into the last commit, keep the message git commit --amend # fold in + edit the commit message while you're at it 3. Special case: only one commit HEAD~1 requires at least 2 commits. Your repo used to have just one, ee01887, so HEAD~1 will throw an error — in that case just run git commit --amend; it naturally means "amend this one commit".4. The lifeline: reflog Whether you mess up with reset or with amend, the history isn't really gone: git reflog # list every HEAD move you've made git reset --hard # go back to any point in time As long as you haven't wiped them out with git gc --prune=now, every commit in the reflog can be recovered. In one sentence: not pushed yet → undo with git reset --soft HEAD~1, add to the last commit with git commit --amend, and if you've wrecked it, check git reflog.5. Cleaning up git repo junkCopygit clean -xdn -e .claude # dry run: see what would be deleted, excluding the .claude directory git clean -xdf -e .claude # delete it
Git commit rollback, for future reference
You made a bad git commit but haven't pushed it yet — how do you go back to the previous commit and keep editing? Since I'm basically an idiot who has to look this up every single time, I had AI throw together the simplest possible guide.
1. Undo the last commit but keep the changes (my case)
The most common need: you committed by mistake, you want to go back to the previous commit, keep working, then commit again.
then just edit your files → git add → git commit to commit again
Three reset modes, and this one table is all you need to remember:
▎ --hard deletes your changes, so be careful. If you want to "go back to the previous commit and keep editing", always use --soft or the default mixed.
2. You just want to add something to the last commit
No need to undo and redo — just fold the new changes in:
3. Special case: only one commit
HEAD~1 requires at least 2 commits. Your repo used to have just one, ee01887, so HEAD~1 will throw an error — in that case just run git commit --amend; it naturally means "amend this one commit".
4. The lifeline: reflog
Whether you mess up with reset or with amend, the history isn't really gone:
As long as you haven't wiped them out with git gc --prune=now, every commit in the reflog can be recovered.
In one sentence: not pushed yet → undo with git reset --soft HEAD~1, add to the last commit with git commit --amend, and if you've wrecked it, check git reflog.
5. Cleaning up git repo junk