Git Best Practices
Guidelines for effective version control.
Commit Practices
Write Good Commit Messages
Format:
Short summary (50 chars max)
Longer description if needed. Wrap at 72 characters.
Explain why, not what (the code shows what).
Good examples:
Add user authentication with JWTFix memory leak in image processingRefactor database connection handling
Bad examples:
Fixed stuffWIPasdf
Commit Often
- Small, focused commits
- One logical change per commit
- Easier to review and revert
Branching Practices
Use Descriptive Branch Names
feature/add-user-search
bugfix/fix-login-timeout
hotfix/patch-sql-injection
Keep Branches Short-Lived
- Merge feature branches quickly
- Delete merged branches
- Avoid long-running branches
Always Branch from Main
git checkout main
git pull
git checkout -b feature/new-feature
Before Committing
Review Your Changes
git diff
git diff --staged
git status
Don't Commit
- Secrets or credentials
- IDE/editor config files
- Build artifacts
- Node modules / dependencies
Use .gitignore:
.env
node_modules/
*.log
.DS_Store
Working with Others
Pull Before Push
git pull --rebase
git push
Never Force Push Shared Branches
# DON'T do this on main/develop!
git push --force
Communicate About Rebases
If you must rebase a shared branch, let your team know.
Useful Git Configurations
# Helpful aliases
git config --global alias.co checkout
git config --global alias.st status
git config --global alias.lg "log --oneline --graph --all"
# Better defaults
git config --global pull.rebase true
git config --global push.autoSetupRemote true
Summary
| Do | Don't |
|---|---|
| Commit often | Make huge commits |
| Write clear messages | Use "WIP" or "fix" |
| Pull before push | Force push shared branches |
| Delete merged branches | Keep old branches |
| Use .gitignore | Commit secrets |
Exercise: Check Your Practices
Review the repository status and log:

