Made a mistake in Git? Relax — Git almost never deletes anything permanently.
Commit to Wrong Branch¶
git branch correct-branch git reset –hard HEAD~1 git checkout correct-branch
Change Commit Message¶
git commit –amend -m “new message”
Forgotten File¶
git add forgotten.py git commit –amend –no-edit
Undo a Commit¶
git reset –soft HEAD~1
Deleted Branch¶
git reflog git checkout -b recovered abc1234
Merge Conflict¶
git status
Git: How to Fix (Almost) Every Mistake¶
git add resolved.py git commit
Revert a Pushed Commit¶
git revert abc1234
Lost Changes After Hard Reset¶
git reflog git branch recovery HEAD@{2}
Ignore a Tracked File¶
git rm –cached config.local.yml echo “config.local.yml” >> .gitignore
Golden Rule¶
Git reflog is your safety net. As long as gc hasn’t run, your data is there.