Git State Recovery
When to Use
Use this skill when git state is confusing, commits seem lost, or you need to recover from a problematic git situation.
First Response: STOP and Assess
Do NOT run any git commands until you understand the current state.
In particular, do not run any command that discards changes (git restore, git checkout --), especially in bulk.
git status
git log --oneline -10
git branch -vv
scripts/git/writer-lock.sh status
Share this output with the user before proceeding.
Recovery Scenarios
Scenario 1: Lost Uncommitted Work
If work was lost due to reset --hard or similar:
Check reflog (saves commits for ~30 days):
git reflogFind the lost commit:
git reflog | grep -i "commit message keyword"Recover by creating a branch:
git branch recovery-branch <commit-hash>
Scenario 2: Detached HEAD
If git status shows "HEAD detached":
Check what you have:
git log --oneline -5If you have work you want to keep, create a branch to attach it:
git checkout -b recovery-branch
Scenario 3: Merge Conflict Overwhelm
If conflicts are too complex:
Abort the merge (safe, no data loss):
git merge --abortDon’t “fix” this by rewriting history. Prefer a plain merge (or ask for human guidance):
git fetch origin --prune git merge <target-branch>
Scenario 4: Wrong Commits on Branch
If commits ended up on wrong branch:
Note the commit hashes:
git log --oneline -5Cherry-pick to correct branch:
git checkout correct-branch git cherry-pick <commit-hash>Clean up the wrong branch safely:
- Prefer abandoning the wrong branch and continuing on the correct one, or
- Revert the commits on the wrong branch:
git checkout wrong-branch git revert <commit-hash>
Safe Commands (Always OK)
| Command | Purpose |
|---|---|
git status |
See current state |
git log |
See commit history |
git reflog |
See all recent HEAD movements |
git branch -a |
List all branches |
git diff |
See uncommitted changes |
Dangerous Commands (Agents: Never)
Never run these as an agent in Base-Shop. If one seems necessary, STOP and ask for human guidance.
| Command | Danger |
|---|---|
git reset --hard |
Loses uncommitted work |
git clean -fd |
Deletes untracked files |
git push --force / -f / --force-with-lease |
Overwrites remote history |
git checkout -- . / git restore . |
Discards local modifications |
git restore -- <pathspec...> / git checkout -- <pathspec...> |
Bulk discards local modifications (multiple paths, directories, or globs) |
git stash drop / git stash clear |
Permanently deletes stashed work |
git rebase (incl. -i) |
Rewrites history (often leads to force-push pressure) |
git commit --amend |
Rewrites the last commit (dangerous after push) |
Common Pitfalls
- Don't panic and run commands without understanding state
- Don't use
reset --hardto "fix" things - Don't force push to fix local issues
- Do run
git statusand share output first - Do create a recovery branch (or checkpoint commit) to protect work
- Do check
git reflogfor recovery options