Common Mistakes in Git and How to Avoid Them

Git is a powerful version control system, but many developers make mistakes that lead to merge conflicts, lost changes, and broken repositories. In this article, we’ll explore the 10 most common Git mistakes and how to avoid them effectively. Let’s dive in! 🚀

🔴 1️⃣ Committing Large or Unnecessary Files

Mistake: Adding Binary or Large Files to Git

Adding large files bloats the repository and slows down clones, pulls, and pushes.

git add large_video.mp4  # ❌ BAD practice
git commit -m "Added large file"

Best Practice: Use .gitignore to Exclude Large Files

Create a .gitignore file and prevent tracking of unnecessary files.

# .gitignore
*.log
node_modules/
*.mp4

🔹 Fix: Use .gitignore to prevent unnecessary files from being committed.

🔴 2️⃣ Forgetting to Pull Before Pushing

Mistake: Pushing Without Pulling Latest Changes

This often leads to merge conflicts and rejected pushes.

git commit -m "Fixed a bug"
git push origin main  # ❌ Push rejected due to outdated branch

Best Practice: Always Pull Before Pushing

Use git pull before pushing to ensure your branch is up to date.

git pull origin main  # ✅ Fetch latest changes before pushing
git push origin main

🔹 Fix: Always pull before pushing to avoid merge conflicts.

🔴 3️⃣ Committing Directly to main or master

Mistake: Making Changes Directly on main

Editing code directly in the main branch risks breaking production.

git checkout main
git commit -m "Fixed issue in production"  # ❌ BAD

Best Practice: Use Feature Branches

Create separate feature branches and merge them into main via pull requests.

git checkout -b feature-login
# Make changes
git commit -m "Added login feature"
git push origin feature-login

🔹 Fix: Never commit directly to main; always use feature branches.

🔴 4️⃣ Committing with Incorrect or Missing Messages

Mistake: Writing Poor Commit Messages

Bad commit messages make it hard to understand changes.

git commit -m "Updated stuff"  # ❌ BAD

Best Practice: Use Descriptive Commit Messages

Follow a structured commit message format.

git commit -m "fix(auth): Resolve login timeout issue"

🔹 Fix: Use clear, structured commit messages.

🔴 5️⃣ Pushing Sensitive Data to Git

Mistake: Committing API Keys, Passwords, or Secrets

Exposing secrets leads to security vulnerabilities.

git add config.properties  # ❌ Contains sensitive API keys
git commit -m "Added API credentials"

Best Practice: Use Environment Variables & .gitignore

Add sensitive files to .gitignore and use environment variables instead.

# .gitignore
config.properties
.env

🔹 Fix: Never commit sensitive information to Git.

🔴 6️⃣ Undoing Mistakes the Wrong Way

Mistake: Using git reset --hard Without Understanding It

This deletes uncommitted changes permanently.

git reset --hard HEAD~1  # ❌ BAD - Deletes last commit permanently

Best Practice: Use git revert or git reset --soft

  • git revert creates a new commit to undo changes.
  • git reset --soft keeps changes staged for editing.
git revert HEAD  # ✅ Safely undo last commit
git reset --soft HEAD~1  # ✅ Keep changes but remove commit

🔹 Fix: Use git revert instead of reset --hard to safely undo mistakes.

🔴 7️⃣ Not Using .gitignore Properly

Mistake: Tracking Unnecessary Files

If you don’t use .gitignore, logs, builds, and environment files may get committed.

git add node_modules/  # ❌ BAD - Tracks unnecessary dependencies

Best Practice: Configure .gitignore Correctly

Use a proper .gitignore file to avoid tracking unnecessary files.

# .gitignore
node_modules/
target/
*.log
.DS_Store

🔹 Fix: Use .gitignore files properly to avoid committing unwanted files.

🔴 8️⃣ Resolving Merge Conflicts Incorrectly

Mistake: Overwriting Changes Instead of Merging

Developers often overwrite changes instead of merging them properly.

git checkout --ours conflicting_file.java  # ❌ Discards other changes

Best Practice: Manually Resolve Conflicts

Use merge tools or edit the files manually.

git mergetool  # ✅ Use merge tool for resolving conflicts

🔹 Fix: Always merge conflicts carefully instead of overwriting them.

🔴 9️⃣ Working on the Wrong Branch

Mistake: Making Changes on the Wrong Branch

Developers often accidentally commit to the wrong branch.

git commit -m "Added new feature"  # ❌ Accidentally committed on `main`

Best Practice: Always Check Your Branch

Use git status and switch to the correct branch before committing.

git branch  # ✅ Shows current branch
git checkout feature-branch

🔹 Fix: Always check your branch before making commits.

🔴 🔟 Force Pushing Without Caution (git push --force)

Mistake: Using git push --force Carelessly

Force pushing overwrites shared history, leading to lost commits.

git push --force origin main  # ❌ BAD - Overwrites remote commits

Best Practice: Use git push --force-with-lease

  • --force-with-lease prevents overwriting others' work.
git push --force-with-lease  # ✅ Safer alternative

🔹 Fix: Avoid git push --force unless absolutely necessary.

✅ Conclusion

By avoiding these 10 common Git mistakes, you’ll work more efficiently and prevent costly errors. 🚀

Which Git mistake have you encountered the most? Let me know in the comments! 🔥

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare