How to Git Commit with Message
A commit is a snapshot of our repository at any point in time. Commits are at the core of version control systems because they store the different versions of our project and make it very easy for us to compare our current version with previous versions and rectify errors. Each commit has a message associated with it that describes the commit. Let's learn how to add these messages to Git commits.
Adding Message to Commits
A commit is a point in the history of our project that describes the state of our repository at that time. It is a good practice to add a descriptive message to our commits. A commit message can be broken into two parts - subject and body. The subject is a short and simple description of the commit. The body is a more detailed explanation of what the commit is about. It is not mandatory to add a body to a message and if a commit is simple enough then just the subject will do the job.
The following are some of the best practices to adopt while writing a commit message.
- The subject of the message should not exceed 50 characters. The subject is just used to summarise the entire commit in a single line.
- Remember to add a blank line between the subject and the main body of the message. This increases the readability of the message.
- Capitalize the subject line and do not end it with a period.
- The body should describe what the commit is about and what changes have been made and the reasons behind it.
- If the commit solves a problem then it should be clearly mentioned. Remember that other developers will view our commits, and we must provide the relevant information about our commits.
- The imperative mood should be used to write the commit message. For example, use "Fix Bug" instead of writing "Fixing Bug". This convention is also followed by commits generated by Git while merging or rebasing.
- If the body contains more than one paragraph then it must be separated by a blank line.
- Follow any additional conventions set by your organization.
Adding messages using the -m flag
Sometimes our commit may not contain any major changes and we do not need to include an entire body for the commit message. In such cases, we can use the -m flag with the Git Commit command to include just the subject of the commit message.
$ git commit -m "Commit Message"
Adding messages using the text editor
The -m flag is convenient if we don't need to include a message-body but that is not always the case. A lot of times our commits will include changes that must be explained using the message body. If we have to add multiple lines, then it will be difficult for us to format the text using the -m flag. We can overcome this problem by running the Git Commit command without any additional flags. This will open our configured text editor where we can enter our commit message.
$ git commit
We will be making commits quite frequently in our repository. Each commit has a message linked with it that explains what the commit is about. A commit message contains a brief one-line subject and a detailed description called the body. Sometimes commits may only have a single line message and in those cases, we will use the -m flag with the Git Commit command to add the message. In other cases, it is better to use the text editor to enter the commit message.