Git & GitHub Tutorial - Part 4 - Change git editor and writing better commit message

Separating Commits

As git reserves all our changes history as by commit. It is better to commit every time when you have done a feature or specific thing. It makes to change the version or roll back or undo something at a very specific point. And this type of commits will show you a better picture of your project progress and would be easy to understand the steps.

If you do multiple features or things at a time and want to commit them you can still commit them separately. Like if change my styles/style.css files for the home page and add a scripts/validate.js file for validating the contact page I can add and commit them separately. To see all changes so far I have done after my last commit I need to run 

git status

Then I can add the styles/style.css file and index.html file to the staging point like:

git add styles/style.css index.html

Then if I run the git status command it will show me staging changes to be committed files and untracked files. Now I can commit the files regarding the home page style changes like

git commit -m "Home page style changes"

This will only commit to the staging files. Now I can add and commit files regarding the contact page. Like 

git add contact.html scripts/validate.js
git commit -m "Validation added for contact page"

Writing good commit message

It is very necessary to write a meaningful commit message. The git recommends writing commit message in the present tense and in the imperative like: "Fix bug" and not "Fixed bug" or "Fixes bug." This convention matches up with commit messages generated by commands like git merge and git revert. In commit message wrap lines at 72 characters and try to describe properly why you made the changes.

Changing the default vim editor

What happens if you write only the git commit command without -m flag? It will open the default vim editor to write your commit. Then you have to press i to got to the insert mode and start typing your commit message. Then to escape writing mode you have to press escape key. Then to exit vim editor, you have to write :wq and press enter.

If you run the git log command you can see the commit you have just made.

The git commit -m "Message" command is good when we write a single line commit message. But when we need to write a long descriptive or multi-line commit message it would be better if we could write it on a text editor. But the default vim editor is confusing and not beginner-friendly. So we will integrate the vs code editor. To integrate vs code for git we need to run the following command

git config --global core.editor "code --wait"

If you want to add other editor you can follow this link and find the command for your editor.

Now if we make any changes and run git add and git commit this will open vs code editor for writing the commit message, and your git bash is waiting to write and save commit message. You can see some commented text in vs code these will not add as a commit message and ignored. Here we can write multi-line commit messages easily. When we save and close the vs code tab our commit will save and show an update in git bash (If not then press enter to your git bash).

If vs code not open then you need to add vs code path in your system. 

If you miss the previous content you can read it from here .

Read: 36
Login to comment

Recent Comments