By Suparna GangulyJune 7th 2021

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!

What’s a Git Commit Message?

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.

Commit Options

  • -m: This sets the Git commit’s message.
  • -a or --all: These automatically commit all modified, tracked, or deleted files.

  • --amend: This option rewrites the end commit. Any changes made recently or a new commit message are included with the existing Git commit messages. --amend flag needs to be performed only on those commit messages that are not pushed to a repository. The commit message should be present in the current repository you’re working on.

Prerequisites to Add a Git Commit Message

  • GitHub Desktop downloaded on your computer: This helps you graphically perform all the git-related tasks. Github.com makes Github Desktop available for Windows, macOS, and  Ubuntu. So, install and configure in order to use this app.

  • Active GitHub account: Do you have an active GitHub account? To check the Git commit message through a remote server you need your own GitHub account. If you don’t have a GitHub account already, Sign up now.

  • Local and Remote Repository: Create a local repository and publish it to your GitHub. This helps look into all the commands used for adding the Git commit message.

Importance of Adding a Good Git Commit Message

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.

How to Add a Git Commit Message

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"

How to Add Multi-line Git Commit Messages

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

"

The Conclusion

And this is the conclusion of the tutorial. Hope you can add a Git commit message now.



)