Git 🧩 5 Git Best Practices 🧩 Troubleshooting
5. Git Best Practices & Troubleshooting
Tips for efficient Git usage and how to recover from common mistakes, building confidence in your Git skills.
a. Git Best Practices
Writing Good Commit Messages
- Concise subject line (50 characters max)
- Blank line separating subject from body
- Detailed body (72 characters per line)
- Focus on what changed and why
Example:Fix login validation error The previous implementation failed to validate email format properly, causing authentication failures for valid users. This commit adds proper regex validation for email addresses.
Atomic Commits
- Commit small, logical, independent changes
- Makes history cleaner and easier to review/revert
- Each commit should represent a single, coherent change
- Example: Fix typo in README, then add new feature, not both at once
Regular Pushing/Pulling
- Keep local and remote repositories in sync
- Avoid large merge conflicts
- Communicate with team about ongoing work
- Push frequently to avoid losing work
b. Git Common Troubleshooting Scenarios
- Undo Last Commit
Soft reset (keeps changes staged)
git reset --soft HEAD~1
Hard reset (loses all changes)
git reset --hard HEAD~1
- Wrong Branch Commit
Remove last commit from current branchgit reset HEAD~1
Stash changes and switch to correct branch
git stash
git checkout correct-branch
git stash pop
- Merge Conflicts
Pull and resolve conflictsgit pull origin mainManual resolution:
- Edit conflicted files
- Mark as resolved: git add file
- Complete merge: git commit
- Remove File from History (DANGER)
Use with extreme caution
git filter-branch --tree-filter 'rm -f sensitive-file.txt' HEAD
- Interactive Rebase
Start interactive rebasegit rebase -i HEAD~3Options in editor: - pick – use commit as-is
- reword – use commit but edit message
- edit – use commit but stop for amending
- squash – merge into previous commit
- drop – remove commit entirely
c. Fixing a Bug and Cleaning
HistoryStep-by-Step Example:
Let’s say you’ve been working on a feature and want to clean up your commit history before submitting a pull request.
- Initial State:
You have 5 commits on your branch:- Add user login functionality
- Fix typo in login form
- Add password strength validation
- Fix database connection issue
- Update documentation
- Goal: Clean up history to have only 2 meaningful commits
- Check current commits
git log --oneline - Start interactive rebase for last 5 commits
git rebase -i HEAD~5 - In the editor, change:
pick 9a1b2c3 Add user login functionality
squash 4d5e6f7 Fix typo in login form
squash 7g8h9i0 Add password strength validation
squash abcd123 Fix database connection issue
squash efgh456 Update documentation - Save and close editor, then edit commit message to be comprehensive
- Push changes (force push required since history changed)
git push origin feature-branch --force-with-lease
d. Questions About Git Best Practices & Troubleshooting
- What are the key principles of writing effective Git commit messages, and why is it important to follow these guidelines?
- How can you ensure that your commits are atomic and meaningful for better code review processes?
- What are the differences between git reset –soft, –mixed, and –hard, and when should each be used?
- Explain how to properly handle merge conflicts in Git and what steps should be taken after resolving them.
- Describe the process of using interactive rebase (git rebase -i) to clean up commit history before submitting a pull request.
- How do you handle situations where you accidentally committed to the wrong branch, and what’s the safest way to correct this?
- What are some best practices for keeping your local repository synchronized with remote changes to avoid conflicts?
- When should you use git stash versus creating a new branch for temporary changes?
- How can you safely remove sensitive files from Git history without breaking the repository?
- What are common mistakes beginners make when working with Git, and how can they be avoided through proper practices?