I want to revisit the git commit command and a few other things...
In the previous chapter we made a few commits and we learned how to check the history of commits using git log commands.
There is a different way of making commits. First we need to make some changes to our existing files from the previous chapter. "You can make whatever changes you want in the index.html file or style.css file".
After making the changes and saving, we can check the current status by using git status, as expected we will see that our file/files have not been staged for a commit. We can set them to the staged status by using the git add command, when we have staged the file/files and check the status again, we can see that we have files to be committed. Awesome!
Now to see how to do a git commit in a different way. To do this we type only git commit and when we press enter, a new file will be opened in our text editor. This is because we set Visual Studio Code as our default text editor when we installed Git. This file is called COMMIT_EDITMSG and it contains a few comments.
We can add our git commit message inside this file just like we do in the terminal. You will notice the terminal says "Waiting for your editor to close the file", all we need to do is add our message to the COMMIT_EDITMSG file, save and close it then we will see the terminal has recognised we added our git commit message.
We can check the short version of the history of our commits using git log --oneline and see that our commit has been captured by git.
What do you think will happen if VS-CODE is not our default text editor, well we can check this by unsetting it as our default text editor inside a file called .gitconfig. The .gitconfig file contains some configuration settings of our project, like our username and email as well as our branch name, but more on that later.
To open the .gitconfig file in the text editor we use the command: code ~/.gitconfig
The ~ (tilde) sign allows us to navigate to the folder of the user
You should know that the .gitconfig file is not located in the project folder, it is located in the user directory because it is a global file and it has access to every git repository. That's why we use the --global flag with the git config command
As you can see at the bottom of the .gitconfig file, the
[core] . All we need to do is remove this from the .gitconfig file and this will unset VS-CODE as our default text editor.
editor = code --wait
But a more professional way of doing this is by running the following command: git config --global --unset core.editor, after running it the
[core] editor = code --wait will be removed from the .gitconfig file.
Time to make a commit. However first we have to make modifications to the file/files in our project directory. Once we make the changes and save, as usual we set the file to the staging area by running the git add command. Finally we can run our git commit command.
When we run the git commit command, we will get a new screen on our terminal, actually this is a default git editor called VIM. You can see there are some comments and like the terminal we are able to insert our message for the git commit command.
To work with VIM is a little out of the scope of this extended chapter. But for those who want to get familiar with VIM, you can explore the cheat sheet here: VIM Cheat Sheet
In short, to insert a message while inside the VIM editor:
- Press i - (This means insert)
- Press ESC - (after entering the comment, to get out of edit mode)
- Press :wq - (to save and quit)
As we can see after we saved and quit, the commit runs successfully and we can check this by running the command: git log --oneline.
To set VS_CODE as our default editor again we need to run the following command: git config --global core.editor "code --wait". This will add the previously removed text back into the .gitconfig file.
When you first setup Git and run the git init command, you may come across a message:
"Using 'master' as the name for the initial branch. This default branch name is subject to change. To configure the initial branch name to use in all of your new repositories, which will suppress this warning, call:".
As stated, this message can be suppressed by running the command: git config --global init.defaultBranch, this command is followed by the name you wish to give your branch.
For example: git config --global init.defaultBranch Branch1
Finally we can list all the configuration settings in our .gitconfig file by running the command: git config --list.
Cool stuff, after this Extended chapter, I think we are ready to move on to Chapter 2. I hope you enjoyed.