Back to blog
March 29, 2024
4 min read

Committing with Meaningful Messages

Learn how to write clear and effective Git commit messages

Committing with Meaningful Messages

Writing clear and meaningful commit messages is crucial for maintaining a clean and understandable Git history. This tutorial will guide you through the best practices for creating effective commit messages that help your team understand the changes made to the codebase.

What You’ll Learn

  • Write clear commit messages
  • Follow commit message conventions
  • Structure commit messages effectively
  • Use commit message templates
  • Understand commit message best practices

Implementation Steps

  1. Basic Commit

    # Create a commit with a message
    git commit -m "Add user authentication feature"
    

    This creates a commit with a descriptive message.

  2. Multi-line Commit Message

    # Create a commit with a detailed message
    git commit -m "Add user authentication feature" -m "- Implement login functionality
    - Add password hashing
    - Create user session management
    - Update documentation"
    

    This creates a commit with a title and detailed description.

  3. Using Commit Template

    # Set up commit template
    git config --global commit.template ~/.gitmessage
    
    # Create template file
    echo "Subject: [50 chars or less]
    
    Body: [72 chars or less]
    
    - What
    - Why
    - How" > ~/.gitmessage
    

    This helps maintain consistent commit message format.

  4. Amending Commit Message

    # Amend last commit message
    git commit --amend -m "New commit message"
    

    This allows you to modify the most recent commit message.

Commit Message Structure

  1. Conventional Commits Format

    <type>(<scope>): <subject>
    
    <body>
    
    <footer>
    

    Types include:

    • feat: New feature
    • fix: Bug fix
    • docs: Documentation
    • style: Formatting
    • refactor: Code restructuring
    • test: Adding tests
    • chore: Maintenance
  2. Example Commit Messages

    feat(auth): add user authentication
    
    - Implement login functionality
    - Add password hashing
    - Create user session management
    
    Closes #123
    

Best Practices

  1. Message Structure

    • Use present tense
    • Keep first line under 50 chars
    • Separate subject from body
    • Wrap body at 72 chars
    • Use bullet points for details
  2. Content Guidelines

    • Be specific and clear
    • Explain what and why
    • Reference issue numbers
    • Include breaking changes
    • Add migration notes
  3. Language and Style

    • Use imperative mood
    • Avoid unnecessary words
    • Be consistent in style
    • Use proper punctuation
    • Follow team conventions
  4. Commit Organization

    • One logical change per commit
    • Keep commits focused
    • Group related changes
    • Consider commit size
    • Follow atomic commits

Common Use Cases

  1. Feature Addition

    git commit -m "feat: add user profile page" -m "- Create profile component
    - Add user information display
    - Implement edit functionality
    - Add profile picture upload"
    
  2. Bug Fix

    git commit -m "fix: resolve login validation issue" -m "- Fix email validation
    - Update error messages
    - Add input sanitization
    - Update tests"
    
  3. Documentation Update

    git commit -m "docs: update API documentation" -m "- Add new endpoints
    - Update request/response examples
    - Fix formatting issues
    - Add authentication details"
    

Advanced Usage

  1. Commit Message Validation

    # Install commitlint
    npm install --save-dev @commitlint/config-conventional
    
    # Configure commitlint
    echo "module.exports = {extends: ['@commitlint/config-conventional']}" > commitlint.config.js
    
  2. Commit Message Templates

    # Create template
    cat > .gitmessage << EOL
    # <type>: <subject>
    # |<----  Using 50 characters  ---->|
    
    # Explain why this change is being made
    # |<----   Try To Limit Each   ---->|
    # |<----   Line to 72 chars    ---->|
    
    # --- COMMIT END ---
    # Type can be
    #   feat     (new feature)
    #   fix      (bug fix)
    #   refactor (refactoring code)
    #   style    (formatting, missing semi colons, etc)
    #   doc      (changes to documentation)
    #   test     (adding or refactoring tests)
    #   chore    (updating grunt tasks etc)
    # --------------------
    # 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 "-" for bullet points in body
    # --------------------
    EOL
    

Common Issues and Solutions

  1. Unclear Messages

    # Before
    git commit -m "fixed stuff"
    
    # After
    git commit -m "fix: resolve login validation issue"
    
  2. Too Long Messages

    # Before
    git commit -m "This is a very long commit message that should be split into multiple lines"
    
    # After
    git commit -m "feat: add user authentication" -m "- Implement login
    - Add validation
    - Update tests"
    
  3. Missing Context

    # Before
    git commit -m "update code"
    
    # After
    git commit -m "refactor: improve authentication flow" -m "- Simplify login process
    - Remove redundant checks
    - Update error handling"
    

Conclusion

Writing meaningful commit messages is essential for maintaining a clean Git history. Remember to:

  • Be clear and specific
  • Follow conventions
  • Structure messages properly
  • Include necessary context
  • Keep messages focused

This knowledge will help you and your team maintain a more organized and understandable codebase.

Next Steps

After mastering commit messages, you might want to:

  • Learn about commit hooks
  • Set up commit message validation
  • Create commit templates
  • Learn about Git workflows
  • Explore Git aliases

Remember that good commit messages are the key to a maintainable codebase.