brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: git reset --soft HEAD~1 undoes the last commit but keeps your changes

Git
~/tips/tip-git-reset-soft-head-undo-commit

git reset --soft HEAD~1

Moves the branch back one commit while the changes stay staged, so you can recommit them differently.

A commit with the wrong message, or with too much in it, does not have to cost any work. The soft reset takes the commit away and leaves the content.

git reset --soft HEAD~1
git status -sb

HEAD~1 is the commit before the current one. With --soft, git moves the branch pointer back and does not touch the index or the working tree. Your changes are therefore still staged, ready to be committed again, with a new message or after you unstage a file.

Know the neighbours, because one of them destroys work:

git reset HEAD~1          # mixed: changes stay, but unstaged
git reset --hard HEAD~1     # DANGER: throws the changes away

The default, --mixed, keeps your file changes but unstages them. --hard resets the files too, and uncommitted work is lost. When in doubt, use --soft first, since you can always go further afterwards.

If you do go too far, git reflog lists where HEAD has been, and the commit can often be recovered from it for a while.

The caveat is a commit that you already pushed. After a reset, your branch is behind the remote, and pushing the new version means rewriting history with a force push. That is fine on a private branch, but a bad idea on a branch that others use. For shared history, git revert is the safer undo.

Further reading: git-reset documentation

More Git tips git show-branch main feature git log --stat -5 git merge --no-ff feature -m "Merge feature" git merge --squash feature git merge --abort All Git tips (144) All topics

Link