{
    "slug": "git_reflog",
    "term": "Git Reflog",
    "category": "git",
    "difficulty": "intermediate",
    "short": "A log of every position HEAD has pointed to — the safety net for recovering commits after a bad reset, rebase, or accidental branch deletion.",
    "long": "The reflog records every time HEAD moves: checkout, commit, reset, merge, rebase. Entries expire after 90 days by default. git reflog shows the history with hashes — you can checkout any hash to recover lost work. This is why 'I accidentally reset --hard and lost my commits' is almost always recoverable. git reflog expire and git gc prune eventually remove entries, but the 90-day default gives a generous recovery window.",
    "aliases": [
        "reflog",
        "git recovery",
        "lost commits"
    ],
    "tags": [
        "git",
        "recovery",
        "version-control"
    ],
    "misconception": "Deleted branches permanently lose their commits — commits are only deleted by gc; reflog gives you the hash to recreate the branch any time within 90 days.",
    "why_it_matters": "The reflog is the answer to almost every 'I accidentally destroyed my work' git emergency — knowing it exists prevents panic and data loss.",
    "common_mistakes": [
        "Panicking and running git commands that make things worse after an accidental reset.",
        "Not knowing that git reset --hard is recoverable via reflog — the commits are still there.",
        "Running git gc --prune=now immediately after a mistake — removes the reflog entries you need for recovery.",
        "Not using reflog to understand what happened before attempting recovery."
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "git_rebase_vs_merge",
        "git_internals",
        "git_bisect"
    ],
    "prerequisites": [
        "git_revert_vs_reset",
        "git_workflows_compared",
        "deployment_rollback"
    ],
    "refs": [
        "https://git-scm.com/docs/git-reflog"
    ],
    "bad_code": "# Accidental hard reset:\ngit reset --hard HEAD~5  # 'I meant HEAD~1!'\n# 5 commits appear lost\n\n# Panicked response:\ngit gc --prune=now  # DO NOT RUN — permanently deletes the commits",
    "good_code": "# Recovery via reflog:\ngit reflog\n# Shows:\n# abc1234 HEAD@{0}: reset: moving to HEAD~5  ← the mistake\n# def5678 HEAD@{1}: commit: feat: add payment webhook  ← lost commit\n# ...\n\n# Recover:\ngit reset --hard def5678   # Go back to before the mistake\n# Or:\ngit checkout -b recovery-branch def5678  # Recover to a new branch",
    "quick_fix": "git reflog shows every HEAD movement for the last 90 days — it's your safety net for recovering from hard resets, bad rebases, and accidentally deleted branches",
    "severity": "info",
    "effort": "low",
    "created": "2026-03-15",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/git_reflog",
        "html_url": "https://codeclaritylab.com/glossary/git_reflog",
        "json_url": "https://codeclaritylab.com/glossary/git_reflog.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 Reflog](https://codeclaritylab.com/glossary/git_reflog) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/git_reflog"
            }
        }
    }
}