🚨 Committed to the wrong branch
❌ Mistake
You meant to work on feature-login, but accidentally committed to main.
🔧 Fix
Check where you are:
git branch
Move commit to a new branch:
git branch feature-login
git reset --soft HEAD~1
git checkout feature-login
Now your commit is safely on the correct branch.
🚨 Forgot to add files before committing
❌ Mistake
You committed but changes are missing.
🔧 Fix
Add missing files and amend commit:
git add .
git commit --amend
🚨 Wrong commit message
❌ Mistake
git commit -m "fix"
🔧 Fix
git commit --amend -m "Fix login validation error"
If already pushed:
git push --force
🚨 Pushed to GitHub accidentally
❌ Mistake
You pushed messy or wrong commits.
🔧 Fix (safe undo)
Reset locally:
git reset --soft HEAD~1
Then force update remote:
git push --force
⚠️ Only do this if you're sure nobody else pulled the code.
🚨 Merge conflict confusion
❌ Mistake
Git shows:
CONFLICT (content): Merge conflict in file.js
🔧 Fix
- Open the file
- Look for:
<<<<<<< HEAD
your code
=======
incoming code
>>>>>>> branch
- Manually choose correct code
- Then:
git add .
git commit
🚨 Accidentally deleted files
❌ Mistake
You removed a file and need it back.
🔧 Fix
Restore file:
git checkout -- filename
Or newer Git:
git restore filename
🚨 Lost commits (panic moment 😱)
❌ Mistake
You reset or switched branches and “lost” work.
🔧 Fix
Recover using reflog:
git reflog
Find commit ID, then:
git checkout <commit-id>
Or restore branch:
git reset --hard <commit-id>
🚨 Wrong branch pushed to GitHub
❌ Mistake
You pushed feature work to main.
🔧 Fix
Create correct branch from commit:
git checkout -b feature-branch
Then reset main:
git checkout main
git reset --hard origin/main
🚨 Forgot to pull before pushing
❌ Mistake
rejected - non-fast-forward
🔧 Fix
Pull first:
git pull origin main
Then push again:
git push
If conflicts happen, fix them then commit.
🚨 Messy commit history
❌ Mistake
Too many “fix fix fix” commits.
🔧 Fix (clean history)
Use interactive rebase:
git rebase -i HEAD~3
Then:
- squash commits
- edit messages
- clean history
🚨 11. Stuck in Vim editor (VERY common 😭)
❌ Mistake
Git opens strange screen and you can’t exit.
🔧 Fix
Exit Vim:
ESC → :wq → Enter (save & exit)
Or:
ESC → :q! → Enter (exit without saving)
🚨 Accidentally staged wrong files
❌ Mistake
You added too many files:
git add .
🔧 Fix
Unstage everything:
git reset
Or specific file:
git restore --staged filename
🧠 Quick “panic commands” cheat sheet
If something goes wrong, try:
git status
git log --oneline
git reflog
git reset --hard HEAD
git restore .
💡 Final Advice
Most Git mistakes are not permanent.
Rule of thumb:
Git almost always has a way back if you use reflog or commits properly.
Top comments (0)