Git Best Practices
This guide outlines best practices for using Git, focusing on repository creation, branching, commit messages, and tagging to help maintain a clean and understandable version history.
Repository Namingβ
Choose a repository name that is descriptive and easy to work with.
| Convention | Recommendation | Example |
|---|---|---|
| Case | Use lowercase. | my-awesome-project |
| Separators | Use hyphens (-). | customer-order-service |
| Length | Keep names concise but descriptive. | web-app |
| Avoid | Underscores (_) or capital letters. | MyProject, my_project |
Branch Namingβ
Use clear and consistent branch names to clarify the purpose of each branch.
Branch Prefixesβ
Categorize branches by their purpose using prefixes.
| Prefix | Purpose | Example Branch Name |
|---|---|---|
main | The primary branch with stable code. | main |
develop | Integration branch for new features. | develop |
feat/ | New feature development. | feat/user-authentication |
fix/ | Bug fixes. | fix/login-form-bug |
docs/ | Documentation changes. | docs/update-readme |
style/ | Code style and formatting changes. | style/format-source-code |
refactor/ | Code refactoring. | refactor/cleanup-api |
test/ | Adding or improving tests. | test/add-user-tests |
chore/ | Maintenance and other tasks. | chore/update-dependencies |
General Guidelinesβ
- Be Descriptive: The branch name should clearly state its purpose.
- Use Hyphens: Separate words with hyphens (
-). - Reference Issues: If applicable, include the issue or ticket number (e.g.,
feat/PROJ-123-add-new-feature).
Commit Messagesβ
A clear, well-structured commit history is essential for project maintainability. Following the Conventional Commits specification creates a standardized history that is easy for both humans and machines to understand.
Commit Message Formatβ
<type>(<scope>): <subject>
<body>
<footer>
-
<type>: The type of change being committed.Type Description Example featA new feature. feat: add user profile pagefixA bug fix. fix: correct login button alignmentdocsDocumentation only changes. docs: update installation guidestyleChanges that do not affect the meaning of the code. style: format code with prettierrefactorA code change that neither fixes a bug nor adds a feature. refactor: simplify user modelperfA code change that improves performance. perf: optimize image loadingtestAdding missing tests or correcting existing tests. test: add tests for user APIchoreChanges to the build process or auxiliary tools. chore: update webpack config -
<scope>(optional): A noun describing the part of the codebase affected by the change.Scope Description Example apiChanges related to the API layer. feat(api): add new user endpointauthChanges related to authentication. fix(auth): correct token refresh logicuiChanges to the user interface. feat(ui): implement dark mode toggledepsDependency updates. chore(deps): update react-router to v6 -
<subject>: A concise description of the change.- Use the imperative, present tense (e.g., "add" not "added").
- Do not capitalize the first letter.
- Do not add a period (
.) at the end.
-
<body>(optional): A more detailed explanation of the change. -
<footer>(optional): Contains information about breaking changes and issue references.
Tagging Best Practicesβ
Tags mark specific points in a repository's history, such as version releases.
Annotated vs. Lightweight Tagsβ
- Lightweight Tags: A simple pointer to a specific commit.
- Annotated Tags: Stored as full objects in the Git database, containing metadata such as the tagger's name, email, date, and a message.
Always use annotated tags for releases, as they contain valuable metadata.
Creating an Annotated Tagβ
Use the -a flag to create an annotated tag with a message. It is a common convention to use Semantic Versioning.
# Create an annotated tag with a message
git tag -a v1.0.0 -m "Release version 1.0.0"
Pushing Tags to Remoteβ
By default, git push does not push tags. You must push them explicitly.
# Push a single tag
git push origin v1.0.0
# Push all local tags
git push origin --tags
Managing Tagsβ
- List Tags:
git tag - Delete a Local Tag:
git tag -d v1.0.0 - Delete a Remote Tag:
git push --delete origin v1.0.0