Tips and Best Practices

Tips and Best Practices

Git Best Practices

Introduction

These tips and best practices will help you use Git more effectively and avoid common mistakes.

Commit Best Practices

Write Good Commit Messages

Short summary (50 chars or less)

Detailed description if needed. Explain what and why, not how.

Fixes #123

Commit Often

Small, frequent commits are better than large ones:

  • Easier to review
  • Easier to revert
  • Better history

Atomic Commits

Each commit should do one thing:

✅ Good: "Add user login feature" ❌ Bad: "Add login and fix bugs and update styles"

Don't Commit Generated Files

Add to .gitignore:

node_modules/
dist/
build/
*.log
.env

Branching Best Practices

Use Descriptive Names

✅ feature/user-authentication
✅ bugfix/login-error-fix
❌ feature/abc
❌ fix/fix

Keep Branches Short-lived

  • Feature branches: days to weeks
  • Delete after merge
  • Rebase onto main regularly

Never Force Push to Main

# BAD
git push --force origin main

GOOD (only in emergencies)

git push --force-with-lease origin main

Collaboration Best Practices

Pull Before Push

git pull origin main

Resolve any conflicts

git push origin feature-branch

Review Before Push

# Check what you're about to push
git log origin/main..HEAD
git diff origin/main..HEAD

Use Pull Requests

  • Smaller PRs are easier to review
  • Include description
  • Link related issues

Daily Workflow

Start of Day

git checkout main
git pull
git checkout -b feature-name

During Work

# Make changes
git add -A
git commit -m "Describe changes"

Repeat

End of Day

git push -u origin feature-name

Useful Commands

Find Bad Commit

git bisect start
git bisect bad
git bisect good v1.0.0

Clean Up Local Branches

# Delete merged branches
git branch -d $(git branch --merged)

Delete remote-tracking gone branches

git fetch -p

Undo Last Commit (Keep Changes)

git reset --soft HEAD~1

See Who Changed What

git blame filename.txt

Stash with Message

git stash push -m "Work in progress"

Common Mistakes

Mistake: Accidental Commit to Main

# Undo but keep changes
git reset --soft HEAD~1
git checkout -b feature-branch

Mistake: Committed Wrong Files

# Remove from commit, keep changes
git reset HEAD~1
git reset HEAD filename-to-remove

Mistake: Forgot to Add File

# Add to last commit
git add forgotten-file.txt
git commit --amend --no-edit

Mistake: Wrong Branch

# Move commits to correct branch
git checkout correct-branch
git cherry-pick commit-sha

Then undo from wrong branch

git checkout wrong-branch git reset --hard HEAD~1

Productivity Tips

Aliases

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.st status
git config --global alias.last 'log -1 HEAD'

Git Prompt

Add branch info to terminal prompt:

# Oh My Zsh

Enable in .zshrc: plugins=(git)

Bash

Add to .bashrc: PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]$(__git_ps1)\$ '

Use IDE Integration

  • VS Code GitLens
  • IntelliJ Git integration
  • GitKraken

Security

Never Commit Secrets

# Add to .gitignore
.env
*.pem
credentials.json
secrets.yaml

Use GitHub Secrets

Store sensitive data in GitHub Secrets, not in code.

Review Before Pushing

Check for sensitive data:

git log -p --all -S "password"
git log -p --all -S "api_key"

Summary

Key takeaways:

  • Write clear, descriptive commit messages
  • Commit often, keep commits small
  • Use descriptive branch names
  • Pull before push
  • Review changes before committing
  • Use aliases for speed
  • Never push secrets
Practice these and your Git workflow will be smooth and efficient.

---

Congratulations! You have completed the Git Fundamentals course.

You now have a solid understanding of:

  • What Git is and why it's essential
  • Installation on various platforms
  • Creating and managing repositories
  • Working with remote repositories and GitHub
  • Branching and merging
  • Resolving conflicts
  • Advanced operations like rebase and reset
  • Popular workflows
  • Automation with hooks and tools
Keep practicing and refer back to these lessons as needed. Happy coding!

Quiz - Quiz - Tips and Best Practices

1. What is an atomic commit?

2. What should you NEVER do with the main branch?

3. What command helps find which commit introduced a bug?

4. What should you never commit to a repository?

5. What is the recommended practice before pushing changes?