Learning How to Git: Creating a (Longer) Commit Message
One of the limitation in creating a Git commit description is that it is limited to 50 characters, which sometimes is not much for a breakthrough changes in our code. To accommodate that, we can use a better way to commit and create the commit message.
Creating Commit with Description
Usually, we will create a commit by using
git commit -m "<some message here>". There is a better way to create commit message, that is by using
git commit (no other parameter). To give a better visualization, let me give you an example.
I’m already making changes in my
README.md file, so now we can add the changes to the staging area using
git add . (or using
git add <file-name> if you prefer). Then after that, we can use
You will be shown a console text editor called Vim. Don’t panic, we only need to use several basic usages of Vim. To change the mode into insert mode, we will need to press the ‘I’ character in our keyboard. Then notice that the lower-left corner will show text ‘-- INSERT --’, that means that now we can write our commit message.
In creating a commit message, there are basically two section: header and body. In Git, the header is limited to 50 characters, anything pass that will be acknowledged as body section. In above example, the header is
Update README.md. Usually, the header body only give a short and concise explanation of what the commit do.
The next section is the body section. In this section, we can add more explanation that is not limited to the 50 header characters. You can create a paragraph as long as you want (I don’t know if it actually have limitation, but as far as I know I never reach that limit). I’m personally prefer to use short points of what the changes do. You can notice that I wrote something like
Add link to <title-of-the-url>. It’s just my preference though.
When you’re done, press
ESC character, then followed by
:wq. Notice that there will be a text
:wq in the lower left corner of Vim.
That is actually a command for
w is for ‘save’, and
q is for quit. The
: character means that anything after that is command. So
:wq means that we want to save and quit the file. After we press enter, and we check the log using
git log, we can see that we already created our commit with that description.
Let’s try to push to our remote repository and see what it looks like on the GitHub repository.
Now you will notice that GitHub handle header and body differently. The upper part is the header and the rest is the body. Which is similar to what we have if we check it using our usual
git log. But, what if we use
git log --oneline. For those who don’t know, adding
— -oneline argument will list a short git commits log.
You will notice that only the header shows up in the one-line git log. This is something to keep in mind, that we have to create a good header on the commit because we use one-line git log most of the time. So, please make the git header message is as clear as possible. That description usually used to explain the changes in more detailed way.
Today, we have created a (longer) commit message that is useful to give better explanation to the changes we made. Next, we will move to branching topics in Git. Hope this story is going to be helpful, thanks for reading!