{
  "id": 8578396,
  "title": "Undo a Git Commit: Two Questions Before You Type Anything",
  "url": "https://urgent.news/2026/09/20/undo-a-git-commit-two-questions-before-you-type-anything",
  "topic": "tech",
  "section": "Tech",
  "published": "2026-09-20T01:02:58.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/jjoyneriv/undo-a-git-commit-two-questions-before-you-type-anything-21o"
  },
  "original_language": "en",
  "account": "When undoing a Git commit, there are four correct answers and three wrong approaches. To determine the right command, you must first answer two key questions: Where is the change? (working tree, staged, committed, or committed and pushed?) and Has anyone else got it?\n\n1. If you edited a file and want to remove the change, the file is in your working tree. Use git restore <file> to revert the change. Example: echo oops config.txt → git status --short → M config.txt → git restore config.txt → Empty output.\n\n2. If you staged something too early and don't want it in the next commit, the change is in the index. Use git restore --staged <file> to unstage the change without touching the file. Example: echo staged config.txt → git add config.txt → git status --short → M config.txt → git restore --staged config.txt → M config.txt again.\n\n3. If you committed too early and the changes are not pushed, the commit exists in your branch. Use git reset --soft HEAD~1 to move the branch pointer back without changing the staged or working tree. This keeps the changes staged for a recommit. Example: git reset --soft HEAD~1 → git status --short → git log --oneline → M config.txt 76ca1c4.\n\n4. If the commit has been pushed and others have pulled it, you cannot simply rewrite history. Instead, use git revert HEAD --no-edit to create a new commit that undoes the changes. Example: git revert HEAD --no-edit → git log --oneline | head -2 → Revert Add line4 6a6f2b6.\n\nAvoid using git reset --hard, as it discards all uncommitted changes and cannot be used after pushing. If you must discard everything, use git reset --hard HEAD forcefully with caution, as it cannot be undone.",
  "summary": "\"How do I undo a commit?\" has four correct answers and three wrong ones, and the difference is not the command — it is two questions you ask before typing anything: Where is the change? Working tree, staged, committed, or committed and pushed ? Has anyone else got it? Answer those and the command picks itself. Below is each situation, run in a disposable repository (git 2.43.0) with real output,…",
  "key_points": [
    "If change is in working tree, use git restore file to revert edit",
    "If staged change is unwanted, use git restore --staged file to unstage",
    "If committed and not pushed, use git reset --soft HEAD~1 to move pointer"
  ],
  "editors_take": null,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "ai_generated": true,
  "disclaimer": "Summaries, key points and the editor’s take are written by software from other outlets’ reporting and may contain errors — always check the linked original."
}