Introduction
This article covers the following tech skills:
🧑💻 New to Git or LabEx? We recommend starting with the Quick Start with Git course.
When working on a project, it is important to have a consistent and informative commit message for each change made to the codebase. This helps other developers understand the purpose of the change and makes it easier to track down bugs or revert changes if necessary. In this lab, you will learn how to set up a commit message template for a Git repository.
Add a Commit Message Template
Without a commit message template, developers may be tempted to write vague or uninformative commit messages, such as “fixed bug” or “updated code”. This makes it difficult for others to understand the purpose of the change and can lead to confusion or mistakes down the line. By setting up a commit message template, developers are encouraged to provide more detailed and informative commit messages, which can improve collaboration and productivity.
For this lab, let’s use the repository from https://github.com/labex-labs/git-playground
. Follow these steps to set up a commit message template for this repository:
- Clone the repository to your local machine using the command
git clone https://github.com/labex-labs/git-playground
. - Navigate to the repository directory using the command
cd git-playground
and configure your GitHub account using the commandsgit config --global user.name "your-username"
andgit config --global user.email "your-email"
. - Create a new file named
commit-template
in the repository directory using the commandvim commit-template
. - Open the
commit-template
file in a text editor and add the following lines:
# <type>: <subject>
# <body>
# <footer>
#This creates a template with three sections, where "<type>" indicates the type of submission, such as "feat" or "fix", "<subject>" is a short #summary describing the content of the submission, "<body>" is a more detailed description, and "<footer>" can contain other metadata, such as the #associated issue number or other comments.
- Press Esc and enter the :wq command, then press Enter to save your changes and exit the
commit-template
file editor. - Use the command
git add commit-template
to addcommit-template
files to the staging area. - Use the command
git config commit.template commit-template
to set thecommit-template
file as the commit message template for the repository. - Use the command
git commit
to open the commit message editor and notice that the commit message editor now contains the commit message template you created in step 4. - Press Esc and enter the :q command, then press Enter to exit the commit message editor.
Summary
In this lab, you learned how to set up a commit message template for a Git repository. By using a commit message template, developers can provide more detailed and informative commit messages, which can improve collaboration and productivity. Remember to always write clear and concise commit messages to help others understand the purpose of your changes.
🚀 Practice Now: Add a Commit Message Template
Want to Learn More?
- 🌳 Learn the latest Git Skill Trees
- 📖 Read More Git Tutorials
- 💬 Join our Discord or tweet us @WeAreLabEx