Common Git Mistakes (And How to Fix Them)
Source: Dev.to
1. Committing on main instead of a feature branch
Fix:
git checkout -b feature-branch
git reset --soft HEAD~1
git commit
This moves your commit onto a new branch without losing changes.
2. Working on the wrong branch
Fix:
git checkout -b new-branch
Your work stays intact.
3. Poor commit messages
Fix:
git commit -m "Fix null pointer error in user login"
Good commit messages save time during reviews and debugging.
4. Accidentally committing sensitive files (e.g., .env)
Fix:
git rm --cached .env
echo ".env" >> .gitignore
git commit -m "Remove env file from repo"
If secrets were pushed, rotate them immediately.
5. Merge conflicts with leftover conflict markers
Fix: Resolve the conflict, remove the >>>>>> markers, then:
git add .
git commit
6. Need to undo a change without rewriting shared history
Fix:
git revert
Never rewrite shared history unless absolutely necessary.
7. Lost a commit after a reset or rebase
Fix:
git reflog
Find the commit hash and recover it:
git checkout
Git almost never truly deletes your work.
8. Using git push --force on a shared branch
Fix:
git push --force-with-lease
This is safer and prevents overwriting others’ work.
9. Creating unnecessary merge commits
Fix:
git pull --rebase
Keeps history clean and avoids unnecessary merge commits.
10. Making multiple unrelated changes in a single commit
Guideline:
- One feature
- One fix
- One idea per commit
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.