In this chapter, we have been using dynamic templates, but Git also has the option of a static commit template, which essentially is just a text file configured as a template. Using the template is very easy and straightforward.
First of all, we need a template. This has to be a text file whose location you should know. Create a file with the following content:
#subject no more than 74 characters please #BugFix id in the following formats #artf [123456] #PCP [AN12354365478] #Bug: 123456 #Descriptive text about what you have done #Also why you have chosen to do in that way as #this will make it easier for reviewers and other #developers.
This is my take on a simple commit message template. You might find that there are other templates out there that prefer to have the bug in the title or at the bottom of the commit message. The reason for having this at the top is that people often tend not to read the important parts of the text! The important part here is the formatting of the references to systems outside Git. If we get these references correct, we can automatically update the defect system as well. Save the files as ~/committemplate
.
We will configure our newly created template, and then we will make a commit that will utilize the template.
To configure the template, we need to use git config commit.template <pathtofile>
to set I,t, and as soon as it is set, we can try to create a commit and see how it works:
$ git config commit.template ~/committemplate
config
file to see that it has been set:$ git config --list | grep template commit.template=c:/Users/Rasmus/committemplate
git config --global
, or it can be set at a local repository level by leaving out the --global
option. We configured our commit template for this repository only. Let's try and make a commit:$ git commit --allow-empty
#subject no more than 74 characters please #BugFix id in the following formats #artf [123456] #PCP [AN12354365478] #Bug: 123456 #Descriptive text about what you have done #Also why you have chosen to do in that way as #this will make it easier for reviewers and other #developers.
It is really as simple as that.
So, in this chapter, we have seen how we can prevent pushing when we have special words in our commit messages. We have also seen how you can dynamically create a commit message with valid information for you or another developer when you are committing. We have also seen how we can build functionality into your own Git by adding small scripts or aliases that all are executed using the Git way. I hope this information will help you to work smarter instead of harder.