KandZ – Tuts

We like to help…!

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 branch
    git reset HEAD~1

Stash changes and switch to correct branch

git stash
git checkout correct-branch
git stash pop
  • Merge Conflicts
    Pull and resolve conflicts
    git pull origin main Manual resolution:
  1. Edit conflicted files
  2. Mark as resolved: git add file
  3. 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 rebase
    git rebase -i HEAD~3 Options 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:
    1. Add user login functionality
    2. Fix typo in login form
    3. Add password strength validation
    4. Fix database connection issue
    5. Update documentation
  • Goal: Clean up history to have only 2 meaningful commits
  1. Check current commits
    git log --oneline
  2. Start interactive rebase for last 5 commits
    git rebase -i HEAD~5
  3. 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
  4. Save and close editor, then edit commit message to be comprehensive
  5. Push changes (force push required since history changed)
    git push origin feature-branch --force-with-lease

d. Questions About Git Best Practices & Troubleshooting

  1. What are the key principles of writing effective Git commit messages, and why is it important to follow these guidelines?
  2. How can you ensure that your commits are atomic and meaningful for better code review processes?
  3. What are the differences between git reset –soft, –mixed, and –hard, and when should each be used?
  4. Explain how to properly handle merge conflicts in Git and what steps should be taken after resolving them.
  5. Describe the process of using interactive rebase (git rebase -i) to clean up commit history before submitting a pull request.
  6. How do you handle situations where you accidentally committed to the wrong branch, and what’s the safest way to correct this?
  7. What are some best practices for keeping your local repository synchronized with remote changes to avoid conflicts?
  8. When should you use git stash versus creating a new branch for temporary changes?
  9. How can you safely remove sensitive files from Git history without breaking the repository?
  10. What are common mistakes beginners make when working with Git, and how can they be avoided through proper practices?

Leave a Reply