Adding messages of modifications to your code is extremely useful. Your code might sound meaningful now, but it might arise doubts after some months or years.
Do you make notes in the mid of your codes? It’s a good practice.
Through this tutorial, you’ll know what’s a Git commit message, how to add single-line and multi-line commit messages, and the importance of adding a Git commit message. Let’s start!
A “commit” command saves all the edits made to a local repository after it’s placed in Git. You make tons of edits that you might not be able to recall later. So, before making the changes to any command line, you have to inform Git about it. A Git Commit Message helps identify all the changes in the documentation.
A well-crafted Git commit message helps communicate about a certain change made in the code to other developers that might be working with you on the same project.
If you don’t know how to add a commit message or keep coding without committing given much importance, years later you may find the modifications weird. Because it can be really difficult to guess the reasons for alterations.
The Git commit messages effectively communicate about the changes, and make development and collaboration easier.
To add a Git commit message follow the instructions below.
Step 1: Open the Terminal. And go to the local repository. Then execute the following command.
git init
This command helps initialize the Git before the commit message is added.
Step 2: Run another command as given below.
git commit
This command checks if there’s any message committed, and lists tracked and untracked files.
Step 3: Create a readme.txt file. This should be in the current repository. To add the file, run this command:
git add readme.txt
Step 4: And the following command adds your first commit message.
git commit -m "Basic tutorials on PHP"
You may not be able to describe a longer change with a single-line comment. So, you need a multi-line commit message for that purpose.
Method 1: Using -m flag multiple times
-m flag helps add multiple paragraphs in the same Git commit message. You can do this by adding -m flag for more than once within your message.
e.g:
git commit index.js -m "Note the Changes" -m "- Fixed a bug"
These multiple messages will appear in the following manner in commit:
Note the Changes
- Fixed a bug
Method 2: Using quotes in the shell
Another method of adding a multi-line Git commit message is using quotes with your message, though it depends on your shell’s capacity. To do this, add single or double quotes before typing the message, keep pressing enter and writing the next line, and finally close the quote at end of the message.
This works fine in Bash, however, it doesn’t let you enter the command until you close the quotes.
e.g:
$ git commit index.js -m "My Changes
- Fixed a critical bug
- Probably added more bugs
"
And this is the conclusion of the tutorial. Hope you can add a Git commit message now.