Skip to content

Latest commit

 

History

History
254 lines (180 loc) · 6.29 KB

File metadata and controls

254 lines (180 loc) · 6.29 KB

💡 Git Tips and Best Practices

Quick tips, reminders, and useful patterns to help you become more efficient with Git.


🧠 General Tips

  • Commit often, but with purpose. Small, meaningful commits make it easier to track changes.
  • Write clear commit messages. Use the imperative mood and describe what and why.
  • Tidy up before sharing. Use git rebase -i to squash, reword, or drop commits before pushing a feature branch.
  • Avoid committing secrets. Use .gitignore to exclude sensitive files like .env.
  • Pull before pushing. Always sync your branch before pushing changes to avoid conflicts.
  • Use branches wisely. Keep main or master clean; do experimental work in feature branches.

🧰 Handy Commands

  • Undo last commit (keep changes):

    git reset --soft HEAD~1
  • Undo last commit (discard changes):

    git reset --hard HEAD~1
    
  • Amend the previous commit:

    git commit --amend
  • Temporarily save uncommitted work:

    git stash
    git stash pop
  • View changes before committing:

    git diff
  • View branch history visually:

    git log --oneline --graph --decorate --all

🚀 Workflow Recommendations

  • Create a dev branch for testing changes before merging to main.

  • Use Pull Requests (PRs) even in solo projects for practice and documentation.

  • Force push safely: If you rebase or amend a shared branch, use git push --force-with-lease instead of --force to prevent overwriting someone else's work.

  • Tag versions using semantic versioning:

    git tag -a v1.0.0 -m "Initial release"
    

🧹 Maintenance Tips

  • Clean up merged branches (local):

    git branch --merged | grep -v "main" | xargs git branch -d
  • Prune remote-tracking branches:

    git remote prune origin

🪄 Pro Tips

Use GitHub Desktop or GitKraken for visualizing commits when learning Git workflows.


📝 .gitignore Best Practices

  • Start with a template: Use gitignore.io to generate .gitignore for your tech stack.

  • Common patterns:

    # Dependencies
    node_modules/
    vendor/
    
    # Environment variables
    .env
    .env.local
    
    # IDE and editor files
    .vscode/
    .idea/
    *.swp
    *~
    
    # OS files
    .DS_Store
    Thumbs.db
    
    # Build outputs
    dist/
    build/
    *.log
  • Don't commit sensitive data: API keys, passwords, tokens should never be in version control.

  • Global gitignore: Create ~/.gitignore_global for personal files:

    git config --global core.excludesfile ~/.gitignore_global

✍️ Commit Message Templates

Create a commit message template for consistency:

1. Create template file ~/.gitmessage:

# <type>(<scope>): <subject>
# |<----  Using a Maximum Of 50 Characters  ---->|

# Explain why this change is being made
# |<----   Try To Limit Each Line to a Maximum Of 72 Characters   ---->|

# Provide links or keys to any relevant tickets, articles or other resources
# Example: closes #23

# --- COMMIT END ---
# Type can be
#    feat     (new feature)
#    fix      (bug fix)
#    refactor (refactoring code)
#    style    (formatting, missing semi colons, etc)
#    docs     (changes to documentation)
#    test     (adding or refactoring tests)
#    chore    (maintain)
# --------------------
# Remember to:
#   - Capitalize the subject line
#   - Use the imperative mood in the subject line
#   - Do not end the subject line with a period
#   - Separate subject from body with a blank line
#   - Use the body to explain what and why vs. how
#   - Can use multiple lines with "-" or "*" for bullet points in body

2. Configure Git to use it:

git config --global commit.template ~/.gitmessage

🪝 Git Hooks Quick Tips

  • Make hooks executable: chmod +x .git/hooks/pre-commit
  • Share hooks with team: Use tools like Husky (Node.js) or pre-commit (Python)
  • Common use cases:
    • pre-commit: Run linters, formatters, tests
    • commit-msg: Enforce commit message format
    • pre-push: Run full test suite

Example pre-commit hook (lint check):

#!/bin/sh
npm run lint
if [ $? -ne 0 ]; then
  echo "❌ Linting failed. Fix errors before committing."
  exit 1
fi

🔍 Code Review Best Practices

  • Keep PRs small: Easier to review, less likely to introduce bugs
  • Self-review first: Review your own changes before requesting review
  • Write descriptive PR descriptions: Include what, why, and how
  • Use draft PRs: For work-in-progress that needs early feedback
  • Respond to feedback: Address comments or explain why you disagree
  • Use conventional comments:
    • nit: - Minor suggestion, not blocking
    • question: - Asking for clarification
    • suggestion: - Proposing an alternative
    • blocking: - Must be addressed before merge

🧼 Repository Hygiene

  • Delete merged branches: Keep repository clean

    git branch -d feature/completed-feature
    git push origin --delete feature/completed-feature
  • Run garbage collection periodically:

    git gc --aggressive --prune=now
  • Keep commits atomic: One logical change per commit

  • Squash fixup commits: Before merging to main

    git rebase -i main
  • Use meaningful branch names: feat/user-auth, not my-branch


🔗 See Also

Explore related Git documentation:


These tips help you maintain clean, efficient, and professional Git workflows every day.