- https://ohshitgit.com/
- Type: reference
- Source: ohshitgit.com
TL;DR
A problem-first cookbook of Git recoveries: find the symptom, copy the commands that undo it.
What it covers
Git’s own docs are hard to search when you do not know the term, so the guide is organized by mistake. The safety net is git reflog; rewriting history (amend, reset) is only safe for unpushed commits, while pushed commits should be undone with git revert. The tone is deliberately blunt and humorous, and the author notes the commands come from experience rather than exhaustive coverage.
Key takeaways
- Undo almost anything with
git reflogto find the state before the mistake, thengit reset HEAD@{index}. - Fix the last commit with
git commit --amend --no-edit(content) orgit commit --amend(message), only if unpushed. - Move a commit to a new branch:
git branch new-branch,git reset HEAD~ --hard,git checkout new-branch. - Move a commit to the right branch:
git reset HEAD~ --soft,git stash, switch branch,git stash pop, commit (orgit cherry-pick). - Undo an older commit with
git revert <hash>, and restore one file withgit checkout <hash> -- path. - Empty
git diffafter edits means the changes are staged: usegit diff --staged. - Nuclear option:
git fetch origin,git reset --hard origin/master,git clean -d --force; destructive and irreversible.
When to use
- When you committed to the wrong branch or with the wrong message.
- When you need to recover lost work after a bad reset or merge.
- When deciding between
resetandrevertfor shared history.