Return to an earlier Git commit
Choose reset, revert, or a detached checkout based on whether you want to rewrite a local branch, undo published work, or inspect an old version.
Use git reset to move a local branch back, git revert to record an undo as new history, or a detached checkout to inspect an old version. For shared, published work, a revert usually avoids disrupting collaborators' existing commits.
Before changing files or branch history, inspect git status and save any uncommitted work you need.
Move a local branch back
Find the desired commit:
git log --oneline
After replacing COMMIT with its ID, choose one of these alternatives:
# Move the branch and discard tracked working-tree and staged changes.
git reset --hard COMMIT
# Move the branch but retain the index and working-tree contents.
git reset --soft COMMIT
--hard can also remove untracked paths that obstruct restoring tracked files. --soft keeps the current staged state; on a clean branch, the changes since the target become staged. Later commits leave the branch's history but may remain reachable through other refs or the reflog.
Undo published commits without rewriting history
For a simple linear sequence, this reverts commits after the target, excluding the target itself:
git revert COMMIT..HEAD
git push
Review the range first with git log COMMIT..HEAD. Reverting can conflict, and a range containing merge commits needs a deliberate mainline choice rather than this generic command. A merge revert also affects what Git will consider already merged in future merges.
After resolving conflicts, stage the resolved files and run git revert --continue. Use git revert --abort to abandon the active sequence. A revert preserves the old commits; it does not make their changes disappear from history or eliminate coordination with teammates.
Inspect an earlier snapshot
git switch --detach COMMIT
# Inspect files or run the old version.
git switch -
This does not move your branch. If you create commits while detached and want to keep them, create a branch pointing to them before leaving.
Recover after an accidental reset
git reflog
Find the previous tip, then first give it a branch name:
git branch recovered-work OLD_COMMIT
You can inspect it and decide how to restore your work without immediately overwriting the current checkout. Recovery depends on the reflog entry and objects still existing; a reflog is not a backup of arbitrary uncommitted files.
FAQ
Why is my push rejected after reset?
If the remote still has the later commits, the update is not a fast-forward. On a shared branch, prefer a revert. A coordinated history rewrite needs a reviewed, scoped --force-with-lease push and can still be prohibited by branch protection.
Can I restore only one file?
git restore --source=COMMIT -- path/to/file
This replaces that working-tree file without moving the branch. Review and stage it if you want a new commit recording the restoration.
What if I chose the wrong target halfway through?
Abort an in-progress revert first. For a completed reset, inspect the reflog and preserve the correct old tip on a recovery branch. Do not repeatedly run hard resets before saving the state you may need.
See git-reset, git-revert, and git-reflog.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan