π¨ 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
reflogor commits properly.
Top comments (0)