Opscraft
Git · Lesson 4 of 8 · 5 min read

Undoing things safely

Pick the right tool

Situation Command Rewrites history?
Discard uncommitted changes in a file git restore file No
Unstage a file git restore --staged file No
Fix the last local commit git commit --amend Yes (local only)
Move branch back, keep changes staged git reset --soft HEAD~1 Yes
Move back, keep changes unstaged (default) git reset --mixed HEAD~1 Yes
Move back and discard changes git reset --hard HEAD~1 Yes — destructive
Undo a commit that's already pushed/shared git revert <sha> No — adds a new inverse commit
Undo a merge commit on main git revert -m 1 <merge-sha> No
Copy a specific commit to another branch git cherry-pick <sha> No (new commit)
Recover something "lost" git reflog then branch/reset —

The interview staple: "How do you undo a bad commit that's already on main?" → git revert, because it creates a new commit and doesn't rewrite shared history; everyone simply pulls it. In GitOps, reverting the commit is also how you roll back a deployment.

Enlarge diagramGIT RESET + FORCE-PUSHGIT REVERT XABXABXX⁻¹X is removed from history. Anyone whoalready pulled it now has a broken clone.A new commit undoes X. History is kept;everyone simply pulls. Safe on main.
Figure 1. Undoing a commit that is already on main. Reset rewrites shared history; revert adds a new commit that cancels the bad one.
Swipe sideways to see all of it, or tap to open it full screen.

Reverting a merge (-m 1 keeps the first parent, i.e. main's side) has a catch: if you later want to re-merge that branch, Git thinks those changes are already merged — you must "revert the revert".

Enlarge diagramBranch (HEAD)Index (staged)Working tree (files)moved backchanges stay stageduntouchedmoved backchanges unstageduntouchedmoved backresetchanges deleted--soft--mixed (default)--hardLost something after --hard? Committed work can usually be found again with git reflog.
Figure 2. All three resets move the branch back. They differ in what happens to your changes; only --hard throws them away.
Swipe sideways to see all of it, or tap to open it full screen.

Force pushes

  • git push --force overwrites the remote branch with your version — anyone else's newer commits there are lost.
  • git push --force-with-lease refuses if the remote branch moved since you last fetched — always prefer it.
  • Protect main and release branches from force pushes and deletion with branch protection rules.
Enlarge diagramgit push --forcegit push --force-with-leaseOverwrites the remote branch with yoursA teammate's newer commits are silently lostOnly overwrites if the remote is where you lastsaw itRefuses if someone pushed in the meantime
Figure 3. Prefer --force-with-lease: it refuses to overwrite commits you have not seen.
Swipe sideways to see all of it, or tap to open it full screen.

Recovering from someone's force-push to main: find the previous tip from your own reflog, a teammate's clone, the CI system's build records, or the hosting platform's activity log/API (GitHub shows force-push events in PRs and the audit log), then push it back (with protection temporarily lifted) and turn on protection.

A secret was committed

  1. Rotate the secret immediately — assume it's compromised the moment it was pushed. Removing it from Git doesn't un-leak it (forks, clones, CI logs, caches).
  2. Remove it from history if required: git filter-repo --invert-paths --path config/prod.env or BFG Repo-Cleaner, then force-push and ask everyone to re-clone; on GitHub, contact support to purge cached views.
  3. Prevent recurrence: pre-commit hooks (gitleaks, detect-secrets), GitHub push protection/secret scanning, .gitignore for env files, secrets from a secret manager instead of files.
Enlarge diagramRotate it nowassume it isalready copiedCheck formisuseCloudTrail andaudit logsRemove fromhistorygit filter-repo orBFGForce-pusheveryonere-clonesPreventpre-commitscanning, pushprotection12345
Figure 4. A secret was pushed. The order matters: rotate first, because removing it from Git does not un-leak it.
Swipe sideways to see all of it, or tap to open it full screen.