This article is not a guide on how to push your code to Git. Instead, it focuses on best practices for writing effective Git commit messages. A Git commit is a critical part of the software development process. Poorly written commit messages can lead to confusion, hinder collaboration, and negatively impact the productivity of a development team.
Learning to write clear and meaningful commit messages is essential for maintaining a high-quality codebase. In this article, I will explore some of the best practices for creating well-structured commit messages, using the Conventional Commits standard as an example. This standard provides a structured approach to writing commit messages, making them more informative and easier to understand for everyone on the team.
By following these guidelines, you can improve the readability and maintainability of your projectโs history, streamline collaboration, and even simplify automation tasks like versioning and changelog generation. Let's directly dive into the main point.
The Basic Convention of a Good Git Commit
A well-structured Git commit message typically follows this format:
<type> [optional scope] : <description>
[optional body]
[optional footer(s)]
Examples of commit types
Commit messages can vary depending on the task you are working on. Here are some common conventions for different scenarios:
-
feat :
New Features-
Description: Introduces a new feature or enhancement.
- Example:
feat(auth): add login functionality. feat(ui) : improve button desing
-
-
fix:
Bug Fixes-
Description: Fixe a bug in the codebase.
- Example:
fix(api): resolve timeout issue fix(ui) : correct alignment of navbar
-
-
docs:
Documentation-
Description: Updates or improvements to documents to documentation (README, code comments, etc.).
- Example:
docs: update README with installation steps docs(api): add missing function descriptions
-
-
style:
Code Styling- Description: Changes that do not affect functionality (formatting, spacing, linting).
style: fix ESLint warnings style: reformat code for consistency
-
perf:
Performance Improvements- Description: Enhances performance without changing functionality.
perf: optimize database queries perf(ui): reduce image loading time
-
refactor:
Code Refactoring-
Description: Improves code structure or readability without changing functionality.
refactor: simplify user validation logic refactor(api): split large functions into smaller ones
-
-
test:
Tests-
Description: Adds or updates test cases.
- Example:
test: add unit tests for login service test(api): update integration tests for payment gateway
-
-
chore:
Maintenance-
Description: Miscellaneous tasks like dependency updates or build changes.
- Example:
chore: update dependencies to latest versions chore(ci): configure GitHub Actions for CI/CD
-
-
build:
Build Changes-
Description: Changes to the build system or external dependencies.
- Example:
build: upgrade webpack to v5 build: add Babel support for modern JS features
-
-
ci:
Continuous Integration-
Description: Updates related to CI/CD pipelines or automation scripts.
- Example:
ci: fix Dockerfile for deployment ci: update GitHub Actions workflow
-
-
revert:
Reverts-
Description: Reverts a previous commit.
- Example:
revert: revert "feat(auth): add login functionality"
-
-
BREAKING CHANGE :
Breaking API/Functionality- Description: A commit introducing a breaking change in the codebase.
-
Usage: Add
!
to the type or includeBREAKING CHANGE
**** in the footer.- Example:
feat(api)!: rename `getUser` to `fetchUser` BREAKING CHANGE: `getUser` is replaced by `fetchUser` to improve naming consistency.
Combination commit messages
In some cases, you may combine multiple changes into one messages:
feat(auth): add forgot password feature and update login API
fix(auth): resolve token expiration issue
Multiline commit messages:
Method 1: Inline with the -m Option
You can provide multiple -m flags to add different lines to your commit message. For example:
git commit -m "feat(auth): add forgot password feature" -m "Added email verification for password resets." -m "Updated login API with enhanced security measures."
This approach is quick but not ideal for detailed or formatted commit messages since it can get cumbersome for longer descriptions.
Method 2: Use a Text Editor for the commit message
- Start the commit without a message:
Run the
git commit
command without the-m
flag:
git commit
- Edit the commit message in Your default text editor:
Your default text editor (like
vim
,nano
or another configured editor) will open, allowing you to write a detailed, multiline commit message. Example in the editor:
feat(auth): add forgot password feature
- Added email verification for password resets.
- Updated login API with enhanced security measures.
Save and close the editor:
After writing the message, save the file and close the editor. The commit will be created with your multiline message.
how to save and exit when writing a commit message in vim
or nano
:
Hereโs how to save and exit when writing a commit message in vim or nano:
In vim :
- Write your commit message: Type the commit message as needed in the editor.
-
Save and exit:
- Press
Esc
to exit insert mode. - Type :
wq
(short for "write and quit") and pressEnter
. - This saves the file and exits
vim
.
- Press
Alternative commands:
- To save without exiting, type
:w
and pressEnter
. - To exit without saving, type
:q!
and pressEnter
.
In nano
Write Your commit Message:
Type the commit message directly into the editor.-
Save and exit:
- Press
Ctrl + O
(Write Out) to save the file. - Press
Enter
to confirm the filename (usually pre-filled). - Then press
Ctrl + X
to exitnano
.
- Press
-
Cancel without saving:
- Press
Ctrl + X
and when prompted to save changes, typeN
for "No."
- Press
Tips for writing effective commit messages
-
Use Imperative Tone (Present Tense) :
- Good:
fix:
resolve issue with pagination.
- Bad:
fixed issues with pagination.
- Good:
-
Be Specific:
- Good:
feat(ui):
add tooltip to icons.
- Bad:
feat: update UI
- Good:
- Keep It Short: Aim for under 50 characters in the header.
Best Wishes.
you can find me on Github : anikakash
Top comments (0)