Undo the last Git commit: soft reset or revert
These blocks describe different cases, not one script. Choose the case that matches your repository. Replace example branch names, file paths and VERIFIED_COMMIT before use. Abort commands apply only to their active operation; do not run both alternatives.
Short answer
To undo a local last commit while keeping its changes, reset --soft moves the branch to its parent and leaves the index and working tree in place. To undo a commit already shared, revert adds a new inverse commit. Decide whether you want to keep the changes and whether other people have the history before choosing a command.
The example and its prerequisites
This example uses one ordinary non-merge commit with a parent, at the current branch tip. The working tree starts clean and no unrelated files are staged. A repository's first commit has no HEAD^ parent, and a merge has multiple parents; neither fits this exact shortcut. Inspect the graph rather than using HEAD~1 as an unexplained universal undo button.
Inspect before changing anything
Read the last commit's patch, status and branch sharing state. You may want to remove the commit wrapper while continuing its code, reverse code that reached a shared branch, or discard the code entirely. Those are different outcomes. Check with collaborators before rewriting a published branch. A recent timestamp or your own author name does not establish that a commit is private.
git status --short
git log -3 --oneline --decorate
git show --stat HEADKeep a reference or a separate checkout
Create rescue/before-undo at HEAD first. It keeps the original committed state reachable for review. Handle any uncommitted files separately; a branch reference does not store them. In this clean example, the commit's patch is the only work that reset --soft exposes as staged. With an already dirty index, the resulting staged diff can combine several tasks and needs separate review.
git branch rescue/before-undo HEAD
SCHOOL_UNDO=$(git rev-parse HEAD)The first workflow
For the local keep-changes case, use reset --soft HEAD^ and inspect git diff --cached. The original commit's content remains in the index and working tree, ready for a revised commit. If you want to edit it unstaged, a mixed reset has different index behavior. Do not switch to --hard merely to make the staged status shorter: hard reset changes working files as well.
git reset --soft HEAD^
git diff --cached
git status --shortChoose the other outcome deliberately
For a shared ordinary commit, save its verified ID as SCHOOL_UNDO and use git revert on that ID. Revert creates another commit whose patch reverses the selected change, retaining the history colleagues already have. Review the result and project checks before an ordinary push. Reverting a merge requires selecting a mainline parent and understanding its later merge effects; use a dedicated procedure for that case.
git revert "$SCHOOL_UNDO"Limits and exceptions
Revert can conflict when later work overlaps the selected change. Read status, resolve and stage the intended files, then use revert --continue. Use revert --abort if the selected change or outcome was wrong. Reset --soft does not have a matching continue/abort state. Avoid mixing the two workflows or starting another history repair while an active revert remains unresolved.
git status
git revert --abortConflicts and cleanup
If you truly intend to discard committed and local file changes, that is an additional destructive decision, not this guide's default. Preserve and inspect the available state first. Uncommitted work is not in reflog. With restore points enabled and local changes detected, FluxGit can save a safety stash before a supported hard reset; this plain terminal sequence does not invoke that protected app path.
Verify the result
After a soft reset, check the new branch tip and the staged patch against the rescue reference. After a revert, check that the new inverse commit affects only the intended behavior and leaves later wanted work intact. Run the relevant project checks. Keep the rescue branch during review and do not infer correctness just because Git completed the command without an error.
git log -4 --oneline --decorate
git status --short
git diff --cachedCommon questions
Will soft reset delete my files? It preserves the working tree and index, while moving the branch reference. Does revert erase the old commit? No, it records a new inverse change. Can reflog recover everything? No; it concerns retained local reference states and committed objects, not arbitrary unsaved file contents.
Review the operation in FluxGit
Use FluxGit’s visual history and patch view to review the operation and the relevant commits. Keep the branch, sharing and local-file checks in this example. The download page lists the currently available builds and platforms.