Writing a good Git commit message is one great thing that has happened to me on my learning journey this year. As simple as it seems, not everyone knows exactly how to write a good commit message. In this article, I'll explain what Git and Github are all about. What exactly is a repository? Why a git commit message, how to write a great git commit message, and tools that can help you write a great commit message
What is a Git?
Git is a version control system that is used for tracking changes to files and coordinating work on those files among multiple people. It is commonly used for source code management in software development, but it can be used to track changes to any set of files. Git allows multiple developers to work on the same codebase simultaneously and helps to resolve conflicts that can arise when changes to the same files are made by different people.
What is a Github?
GitHub is a web-based platform for hosting and collaborating on Git repositories. It provides a user-friendly interface for working with Git, and it allows users to easily share their code and collaborate with others on projects. Many open-source software projects are hosted on GitHub, and it is a popular platform for developers to share and collaborate on code.
What is a Git Commit Message?
The commit message is used to explain what changes were made in a git repository, why those changes were made, and any other relevant information about the changes. The commit message is important because it provides context and information about the changes that were made, and it makes it easier for other people who are working on the same codebase to understand the history and progress of the project.
What is a Git repository?
It is a data structure that stores all of the information about a project, including all of the files, the entire history of changes to those files, and metadata about the project and its contributors. A Git repository is typically stored on a remote server and can be accessed by multiple people, allowing them to collaborate and work on the project simultaneously. A Git repository can be used to track changes to any set of files, but it is commonly used for source code management in software development.
Why is the Commit Message Important?
- Git commit messages are an important part of a project's collaboration and documentation processes. It is good practise to write clear, descriptive commit messages that accurately reflect the changes made in a commit and provide context for other developers.
- Git commits provide details for the changes made in a commit. By reading the commit message, other people who are working on the project can understand why a particular change was made and how it fits into the overall development of the project.
- Git commit serves as a way to document the history of the project. When you look back through the commit history of a project, the commit messages can provide valuable details and insight into the decisions that were made and the thought process behind the development of the project.
- Git commit can be used to generate change logs or release notes. Many projects will use tools to automatically generate a list of changes made between versions, and the commit messages are often used as the source of this information. Employers always do this.
How to Write a Good Commitment Message
Adopting an Atomic Git Commit Strategy
Atomic commits refer to the practise of making changes to a Git repository in a way that ensures that each commit is a self-contained unit of work. In other words, every line of code that is doing a specific task should have a commit message; a function or method that is going to achieve a specific task should have a commit message.
An atomic commit should contain only related changes that can be reviewed and understood independently from other commits. This is your best option as compared to making multiple unrelated changes in the same commit or making a series of commits that are not independent and require each other in order to be understood.
Adopting an Atomic Git Commit Strategy
There are several benefits to using atomic commits:
They make it easier to understand the history of a project. By reviewing a series of independent commits, it is easier to see the progression of a project and understand how it has evolved over time.
They make it easier to review changes. By reviewing a single commit, it is easier to understand the purpose and impact of the changes that were made. This can make code review more efficient and help identify problems earlier in the development process.
They make it easier to revert changes. If a commit is self-contained and independent, it is easier to revert just that commit without affecting any other changes. This can be useful if a problem is discovered after a commit or if a change needs to be undone for any reason.
Overall, using atomic commits can help to improve the clarity, readability, and maintainability of a Git repository. It is a good practise to strive for when working on a project, especially when working with a team of developers.
How To Write Good Commit Message?
Keep the message short and to the point. A good commit message should be brief and focus on the key changes that were made in the commit.
Use the present tense and imperative mood. A good commit message should use the present tense (e.g., "change" instead of "changed") and the imperative mood (e.g., "fix" instead of "fixes").
Be specific and describe the changes in detail. A good commit message should provide a clear and detailed description of the changes that were made, including any relevant details about why the changes were necessary and how they were implemented.
Use clear and descriptive language. A good commit message should use language that is easy to understand and that accurately describes the changes that were made. Avoid using technical jargon or abbreviations that may not be familiar to everyone who is working on the project.
provide context and background information. A good commit message should provide any relevant context or background information that will help other people understand the changes that were made and why they were necessary. This can include information about the problem that the changes are intended to solve or any relevant background information about the code or the project.
Tools That Can Help You Write a Great Commitment Message
ย
There are tools out there that can help you write great commit messages. We will just few below
GitLens: GitLens is an extension that I enjoy using; I have it installed on PyCharm, InteliJ, and Visual Studio. It enhances the built-in Git capabilities in Visual Studio Code. It allows me to visualise code authorship at a glance via Git blame annotations and code lens, seamlessly navigate and explore Git repositories, gain valuable insights via powerful comparison commands, and so much more. GitLens supercharges the Git capabilities built into Visual Studio Code.
Git Commit -v: This one allows you to write the commit message and the commit message body at the same time, in the same file. This can be useful because it allows you to include more detailed information about the commit in the message body.
Commitlint: This is a tool that checks your commit messages for style and formatting errors. It can enforce rules such as requiring a certain format for the subject line or requiring that the subject line be a certain length.
Conventional Commits: This is a specification for commit messages that defines a standard format for commit messages. It can be used with tools like commitlint to enforce a consistent style for commit messages.
Commitizen: This is a command-line tool that helps you write commit messages using a prompt-based workflow. It can guide you through the process of writing a commit message and enforce rules such as requiring a certain format for the subject line.
Wrapping-Up
The key to writing a good commit message is to provide clear and concise information about the changes that were made and why they were necessary. It's a good idea to follow best practises for writing commit messages, such as keeping the subject line short and to the point and including a detailed explanation of the changes made in the commit message body. This can help you read and understand your commit history.
This will help other people who are working on the same codebase understand the history and progress of the project, which will make it easier for them to collaborate and contribute to it.
Your feedback is highly appreciated.
you can follow me on twitter and linkedIn
Top comments (0)