- Keep commits small and atomic
- Each commit should have a single responsibility (e.g., "Fix bug in user authentication" instead of "Fix bugs and refactor database queries").
- Write meaningful commit messages
- Follow the convention:
Example:
<type>: <short description> <optional longer description>feat: Add user authentication via JWT - Implement login with JWT tokens - Store tokens securely in HTTP-only cookies
- Follow the convention:
- Commit only relevant changes
- Avoid committing unnecessary files (e.g., IDE settings, logs). Use
.gitignoreto prevent them from being tracked.
- Avoid committing unnecessary files (e.g., IDE settings, logs). Use
- Use a branching strategy
- GitFlow (for complex projects)
- GitHub Flow (for simpler projects)
- Trunk-based development (for fast-paced CI/CD environments)
- Use descriptive branch names
- Example:
feature/user-authenticationbugfix/fix-login-redirecthotfix/security-vulnerability
- Example:
- Rebase instead of merging for clean history
- Use
git rebasefor feature branches before merging to avoid unnecessary merge commits. - Example:
git checkout feature-branch git rebase main
- Use
- Avoid long-lived branches
- Keep feature branches short-lived and merge them quickly to avoid conflicts.
- Use Pull Requests (PRs) for code reviews
- Enforce PR reviews before merging.
- Provide clear descriptions of changes.
- Squash and merge for cleaner history
- Helps keep the commit history readable.
- Resolve conflicts properly
- Don't blindly accept incoming changes. Carefully review and test merged code.
- Never commit secrets
- Use environment variables or secret management tools instead.
- Consider using
git-secretsorpre-commit hooksto prevent accidental secret commits.
- Sign commits (optional but good for security)
- Use GPG or SSH to sign commits for authenticity.
- Example:
git commit -S -m "feat: Implement secure authentication"
- Optimize repository size
- Avoid committing large binary files (use Git LFS if needed).
- Regularly prune and clean up old branches.
- Use
git stashto save work-in-progress changes - Undo mistakes with caution
git reset --soft HEAD~1(Undo last commit but keep changes)git checkout -- <file>(Discard unstaged changes)git reflog(Recover lost commits)