{
    "slug": "git_revert_vs_reset",
    "term": "Git Revert vs Reset vs Restore",
    "category": "git",
    "difficulty": "intermediate",
    "short": "Three different ways to undo in git — revert creates a new undo commit (safe for shared branches), reset moves HEAD (rewrites history, dangerous on shared branches), restore undoes working directory changes.",
    "long": "git revert <hash>: creates a new commit that undoes the changes — safe for shared branches, preserves history. git reset HEAD~1: moves HEAD back, three modes — --soft (keep changes staged), --mixed (keep changes unstaged, default), --hard (discard changes entirely). git restore: undoes working directory or index changes without touching commit history. Rule: never use reset --hard on commits pushed to a shared branch — use revert instead. git reflog recovers from accidental hard resets.",
    "aliases": [
        "git revert",
        "git reset",
        "git restore",
        "undo commit"
    ],
    "tags": [
        "git",
        "workflow",
        "version-control"
    ],
    "misconception": "git reset --hard is the same as git revert — reset rewrites history by moving HEAD; revert adds a new commit. Using reset --hard on a shared branch forces all collaborators to reset too.",
    "why_it_matters": "git reset --hard on a branch shared with teammates forces them to do git reset --hard too or end up with conflicts — revert is always safe on shared branches.",
    "common_mistakes": [
        "git reset --hard on a pushed branch — rewrites public history, breaks collaborators.",
        "git revert when you meant reset — revert on a local-only branch adds unnecessary noise.",
        "Not knowing that git restore --staged unstages without losing changes.",
        "Using reset to 'undo' a merge instead of git revert -m 1 <merge-hash>."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "git_reflog",
        "git_stash_advanced",
        "git_hooks_deep"
    ],
    "prerequisites": [
        "git_workflows_compared",
        "deployment_rollback",
        "git_bisect"
    ],
    "refs": [
        "https://git-scm.com/docs/git-revert"
    ],
    "bad_code": "# Dangerous: reset on shared branch:\ngit reset --hard HEAD~3  # Removes 3 commits locally\ngit push --force         # Rewrites shared history\n# Teammates: their local branches now diverge\n# They must reset or face complex merges",
    "good_code": "# Safe: revert on shared branch:\ngit revert abc123      # Creates new 'undo' commit — history preserved\ngit push               # Safe — no history rewrite\n\n# Local cleanup only (never pushed):\ngit reset --soft HEAD~1   # Undo commit, keep changes staged\ngit reset --mixed HEAD~1  # Undo commit, keep changes unstaged\ngit reset --hard HEAD~1   # Undo commit AND discard changes (careful!)\n\n# Undo working directory changes:\ngit restore src/file.php  # Discard unstaged changes to one file\ngit restore --staged src/ # Unstage without losing changes",
    "quick_fix": "Use git revert to undo commits on shared branches (creates a new commit, safe to push); use git reset only on local/private branches (rewrites history, dangerous to force-push)",
    "severity": "high",
    "effort": "low",
    "created": "2026-03-16",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/git_revert_vs_reset",
        "html_url": "https://codeclaritylab.com/glossary/git_revert_vs_reset",
        "json_url": "https://codeclaritylab.com/glossary/git_revert_vs_reset.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Git Revert vs Reset vs Restore](https://codeclaritylab.com/glossary/git_revert_vs_reset) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/git_revert_vs_reset"
            }
        }
    }
}