Skip to content

Latest commit

 

History

History
83 lines (70 loc) · 2.73 KB

File metadata and controls

83 lines (70 loc) · 2.73 KB

Git Best Practices


1. Commit Best Practices

  • 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:
      <type>: <short description>
      
      <optional longer description>
      
      Example:
      feat: Add user authentication via JWT
      
      - Implement login with JWT tokens
      - Store tokens securely in HTTP-only cookies
      
  • Commit only relevant changes
    • Avoid committing unnecessary files (e.g., IDE settings, logs). Use .gitignore to prevent them from being tracked.

2. Branching and Workflow Best Practices

  • 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-authentication
      • bugfix/fix-login-redirect
      • hotfix/security-vulnerability
  • Rebase instead of merging for clean history
    • Use git rebase for feature branches before merging to avoid unnecessary merge commits.
    • Example:
      git checkout feature-branch
      git rebase main
  • Avoid long-lived branches
    • Keep feature branches short-lived and merge them quickly to avoid conflicts.

3. Collaboration Best Practices

  • 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.

4. Security and Performance Best Practices

  • Never commit secrets
    • Use environment variables or secret management tools instead.
    • Consider using git-secrets or pre-commit hooks to 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.

5. Handling Mistakes Gracefully

  • Use git stash to 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)