Core Git 6/8

Undo And Recovery

Recover fast without damaging shared history. Choose the command based on what layer you need to fix: use restore for file-level undo, revert for shared history-safe rollback, and reset only when rewriting private local history is acceptable. Prefer the least destructive option first: restore file changes, revert shared commits, reset only private local history, use reflog for rescue, and let FlexDeploy handle tracked version restore when deployment recovery is the real need.

Back to Git Concepts overview

What Git Reset Does

git reset moves the current branch and HEAD to another commit. By itself, reset does not create a new commit like git revert does. Instead, it rewrites your local branch position, which is why it is usually safe only on private local history.

The second part of reset is the mode. --soft keeps changes staged, --mixed unstages them but keeps the file edits, and --hard rewrites both the index and working tree to match the target commit. If you do not specify a mode, git reset means git reset --mixed.

Reset To A Specific Commit

Use a specific commit hash when you want to name the exact commit that HEAD and the branch should move to. The reset mode still decides what happens to your staged and unstaged local changes afterward.

COMMAND RUN > git reset --soft b2c3 a1b2 b2c3 c3d4 d4e5 orphaned orphaned main HEAD reset moves HEAD + branch to commit b2c3 --soft: move HEAD only, keep changes staged and on disk. Explicit hash form: reset directly to commit c3d4.

Reset Relative To HEAD

Use a relative reference like HEAD~1 when you want to move back from the current commit by position instead of naming a hash directly. The reset mode still decides what happens to your staged and unstaged local changes afterward.

COMMAND RUN > git reset --soft HEAD~1 a1b2 b2c3 c3d4 d4e5 orphaned main HEAD reset moves HEAD + branch back one commit --soft: move HEAD only, keep changes staged and on disk. Use on private local history only.

How Git Revert Works

git revert does not move a branch backward. Instead, Git reads an existing commit, calculates the opposite change, and writes a brand new commit that undoes it. That keeps history linear and auditable, which is why revert is usually the right choice on shared or protected branches.

COMMAND RUN > git revert c3d4 a1b2 b2c3 c3d4 d4e5 r5u6 commit to undo +10 lines revert commit -10 lines
git revert c3d4
git push origin main

Restore: File-Level Undo

git restore writes file content back into your working tree or staging index without creating a new commit. Use plain git restore to discard a local file edit, or git restore --staged to remove a file from the next commit snapshot while leaving the working copy alone.

COMMAND RUN > git restore src/webhook.js WORKING TREE Files On Disk what you are editing locally M src/webhook.js local edits will be discarded working tree is rewritten from Git's saved copy STAGING INDEX Next Commit Snapshot what Git will package next src/webhook.js matches HEAD staged version stays as-is default restore reads from the index first LOCAL REPOSITORY Saved History committed snapshots only commit 9fab23 src/webhook.js in last saved snapshot history does not move restore file from index history unchanged Plain git restore rewrites the working file from the staged copy without creating a commit.
git restore src/webhook.js
git restore --staged src/webhook.js

Two Rollback Options: Deploy State vs Git History

Fast environment recovery can be handled by your deployment automation tool or CI/CD release platform. Git history correction should still be PR-governed.

Lane 1: Environment rollback (deployment tool) Current prod version Select previous version Deploy restore Lane 2: Git history correction (PR flow) Create revert/fix branch Open and approve PR Merge to protected main
git switch -c revert/fix-issue
git revert <sha> || apply forward fix
git push -u origin revert/fix-issue

Reflog Rescue For Lost Commits

git reflog shows where HEAD and your branch references used to point, even after reset, checkout, or accidental branch moves. If a commit seems lost locally, reflog is often the fastest way to find that older pointer and create a recovery branch before Git garbage collects unreachable objects.

Reflog timeline HEAD@{0}: reset --hard 9a1f HEAD@{1}: commit: feat checkout improvements HEAD@{2}: checkout: moving from feature/payments to main
git reflog
git checkout -b recovery HEAD@{1}
Previous Working Tree + Staging All Concepts Back To Overview Next Merge + Rebase