DEV Community

Cover image for Common Git Mistakes (And How to Fix Them)
Shamim Ali
Shamim Ali

Posted on

Common Git Mistakes (And How to Fix Them)

Even experienced developers make mistakes with Git. The good news is that most Git mistakes are recoverable if you know what to do. This post covers some of the most common Git problems and how to fix them safely.

1. Committing to the Wrong Branch

The mistake:
You accidentally commit changes directly to main instead of a feature branch.

The fix:

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

This moves your commit onto a new branch without losing changes.

2. Forgetting to Create a Branch

The mistake:
You start coding before creating a branch.

The fix:
Just create a branch at your current commit:

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

Your work stays intact.

3. Messy or Useless Commit Messages

The mistake:
Using messages like fix, update, or changes.

The fix:
Use clear, descriptive messages:

git commit -m "Fix null pointer error in user login"
Enter fullscreen mode Exit fullscreen mode

Good commit messages save time during reviews and debugging.

4. Accidentally Committing Sensitive Files

The mistake:
You commit .env, API keys, or credentials.

The fix:

git rm --cached .env
echo ".env" >> .gitignore
git commit -m "Remove env file from repo"
Enter fullscreen mode Exit fullscreen mode

If secrets were pushed, rotate them immediately.

5. Merge Conflicts You Don’t Understand

The mistake:
You panic when Git shows conflict markers.

The fix:
Open the conflicted file and resolve:

<<<<<<< HEAD
your code
=======
their code
>>>>>>> branch-name
Enter fullscreen mode Exit fullscreen mode

Choose the correct logic, remove the markers, then:

git add .
git commit
Enter fullscreen mode Exit fullscreen mode

6. Pushing Broken Code

The mistake:
You push code that breaks the build.

The fix:
Create a quick fix commit or revert:

git revert <commit-hash>
Enter fullscreen mode Exit fullscreen mode

Never rewrite shared history unless absolutely necessary.

7. Losing Work After a Reset

The mistake:
You think your changes are gone forever.

The fix:
Use Git’s safety net:

git reflog
Enter fullscreen mode Exit fullscreen mode

Find the commit and recover it:

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

Git almost never truly deletes your work.

8. Force Pushing to Shared Branches

The mistake:
Using git push --force on a shared branch.

The fix:
If absolutely required, use:

git push --force-with-lease
Enter fullscreen mode Exit fullscreen mode

This is safer and prevents overwriting others’ work.

9. Not Pulling Before You Push

The mistake:
You push without syncing first.

The fix:

git pull --rebase
Enter fullscreen mode Exit fullscreen mode

This keeps history clean and avoids unnecessary merge commits.

10. Treating Git Like a Backup Tool

The mistake:
Huge commits with many unrelated changes.

The fix:
Commit early and often:

  • One feature
  • One fix
  • One idea per commit

Conclusion

Git mistakes happen to everyone, what matters is knowing how to recover. Once you understand how Git tracks history, it becomes a powerful safety net rather than a source of stress.

Learn the tools, respect shared branches, and don’t panic, Git is usually on your side.

Top comments (0)