DEV Community

Kyle Y. Parsotan
Kyle Y. Parsotan

Posted on

Here are the most common Git mistakes beginners make and exactly how to fix them. Think of this as your β€œGit emergency guide.”

🚨 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
Enter fullscreen mode Exit fullscreen mode

Move commit to a new branch:

git branch feature-login
git reset --soft HEAD~1
git checkout feature-login
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

🚨 Wrong commit message

❌ Mistake

git commit -m "fix"
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Fix

git commit --amend -m "Fix login validation error"
Enter fullscreen mode Exit fullscreen mode

If already pushed:

git push --force
Enter fullscreen mode Exit fullscreen mode

🚨 Pushed to GitHub accidentally

❌ Mistake

You pushed messy or wrong commits.

πŸ”§ Fix (safe undo)

Reset locally:

git reset --soft HEAD~1
Enter fullscreen mode Exit fullscreen mode

Then force update remote:

git push --force
Enter fullscreen mode Exit fullscreen mode

⚠️ 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
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Fix

  1. Open the file
  2. Look for:
<<<<<<< HEAD
your code
=======
incoming code
>>>>>>> branch
Enter fullscreen mode Exit fullscreen mode
  1. Manually choose correct code
  2. Then:
git add .
git commit
Enter fullscreen mode Exit fullscreen mode

🚨 Accidentally deleted files

❌ Mistake

You removed a file and need it back.

πŸ”§ Fix

Restore file:

git checkout -- filename
Enter fullscreen mode Exit fullscreen mode

Or newer Git:

git restore filename
Enter fullscreen mode Exit fullscreen mode

🚨 Lost commits (panic moment 😱)

❌ Mistake

You reset or switched branches and β€œlost” work.

πŸ”§ Fix

Recover using reflog:

git reflog
Enter fullscreen mode Exit fullscreen mode

Find commit ID, then:

git checkout <commit-id>
Enter fullscreen mode Exit fullscreen mode

Or restore branch:

git reset --hard <commit-id>
Enter fullscreen mode Exit fullscreen mode

🚨 Wrong branch pushed to GitHub

❌ Mistake

You pushed feature work to main.

πŸ”§ Fix

Create correct branch from commit:

git checkout -b feature-branch
Enter fullscreen mode Exit fullscreen mode

Then reset main:

git checkout main
git reset --hard origin/main
Enter fullscreen mode Exit fullscreen mode

🚨 Forgot to pull before pushing

❌ Mistake

rejected - non-fast-forward
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Fix

Pull first:

git pull origin main
Enter fullscreen mode Exit fullscreen mode

Then push again:

git push
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

Or:

ESC β†’ :q! β†’ Enter   (exit without saving)
Enter fullscreen mode Exit fullscreen mode

🚨 Accidentally staged wrong files

❌ Mistake

You added too many files:

git add .
Enter fullscreen mode Exit fullscreen mode

πŸ”§ Fix

Unstage everything:

git reset
Enter fullscreen mode Exit fullscreen mode

Or specific file:

git restore --staged filename
Enter fullscreen mode Exit fullscreen mode

🧠 Quick β€œpanic commands” cheat sheet

If something goes wrong, try:

git status
git log --oneline
git reflog
git reset --hard HEAD
git restore .
Enter fullscreen mode Exit fullscreen mode

πŸ’‘ 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)