Git Best Practices
Following consistent conventions makes your codebase easier to understand and collaborate on.
Commit Messages
Use the imperative mood, keep the subject under 50 characters, and separate with a blank line:
Add user authentication
- Implement login form component
- Add JWT token handling
- Update API middleware
The subject line is what shows up in git log --oneline. Write it like a command: “Add feature” not “Added feature”.
Atomic Commits
Each commit should do one thing. If you fix a bug and refactor a function, those should be two separate commits. This makes it easier to:
- Review changes in pull requests
- Revert specific changes with
git revert - Understand the history with
git log
Branching Strategy
git checkout -b feature/user-auth
git checkout -b fix/login-redirect
git checkout -b chore/update-dependencies
Use descriptive branch names with prefixes: feature/, fix/, chore/, docs/. Keep main stable and always deployable.
Pull Request Workflow
- Create a feature branch from
main - Commit changes with clear messages
- Push and open a PR
- Get review and address feedback
- Merge and delete the branch
Code Review Tips
- Keep PRs small and focused on one change
- Write a clear PR description explaining what and why
- Address all review comments before merging
- Use
git rebase -ito clean up commits before merging
Protecting Your Work
- Pull before starting new work to avoid conflicts
- Run tests locally before pushing
- Use
git stashwhen you need to switch contexts quickly - Never force-push to shared branches
Checklist
- Add
.gitignorebefore your first commit - Never commit secrets, keys, or credentials
- Keep commits small and focused
- Write meaningful commit messages
- Use branches for all non-trivial work
- Review diffs before committing with
git diff - Pull before starting new work to avoid conflicts
Common Mistakes
- Committing directly to
mainfor new features - Writing vague messages like “fix” or “update”
- Bundling unrelated changes in one commit
- Leaving stale branches unmerged and undeleted
- Forgetting to pull before starting new work
- Not using
.gitignoreand committingnode_modules/