This repository demonstrate how to install commitlint and use Gitmoji as the Git Commit specification.
npm@10.22 or higher.
The following Commit Specification
paragraph is recommended to be copied to the README
of your project repository,
and then the installation steps please refer to the paragraph CommitLint Install Steps
.
You need to execute
npm install
in this project root directory to installcommitlint
,husky
tools (used to standardize Git commit messages).
This project uses Gitmoji as the Git commit message specification, and the rules are as follows:
emoji subject(#ID)
BLANK LINE
Message Body
BLANK LINE
Footer
The specific rules are as follows:
- Commit Message must start with an actual emoji UTF-8 character (not gitmoji code).
- A Commit does only one thing (A simple criterion is that the title of the Commit Message must be able to fully summarize all the changes in this commit)
- Header is required. It is recommended to use English, capitalize the first letter, and do not add a period at the end of the Header sentence; Body and Footer can be omitted.
- Header must contain only one type. Please refer to Gitmoji for the available types.
- The header length should be between 15 and 100 characters.
- If Commit resolves the issue, please end with (#ID) at the end of the header, and then close the Issue in the Body.
- Use Markdown grammar to write in the body.
- The verb uses the general present tense. ("Add feature" instead of "Added feature")
- Use imperative grammar. ("Move cursor to..." instead of "Moves cursor to...")
- Explain "what", "why" and "how" in the body.
- All WIP (Work In Progress) Commits must have WIP 🚧 Emoji.
# Good
🙈 Update the project root .gitignore file
🏗 Enable bitcode for iOS
🚧 Add temporary testing code
Each Gitmoji has its specific meaning, such as ✨ -> Introduce new features
, 📝 -> Add or update documentation
, 🐛 -> Fix a bug
Please choose the appropriate one according to what you did in the commit Gitmoji
It is recommended to install the Gitmoji App or plug-in for easy search and copy emoji. (You can also click to copy emoji directly on the web page)
-
Gitmoji App for macOS / Windows / Linux
It is recommended to install this app on desktop.
-
After the search box is filtered (for example, search
feat
to filter out the corresponding ✨ Gitmoji)Click the corresponding Gitmoji icon to directly copy the emoji to the clipboard.
-
Execute the following commands in the repo root directory to install node dependent libraries and generate
package.json
andpackage-lock.json
files. (You need to commit these two files to git, then other developers only need to executenpm install
)npm install --save-dev @commitlint/{config-conventional,cli} husky
Remember to add the
node_modules
directory to.gitignore
after installation -
Execute the following commands in the repo root directory to initialize husky and create a husky (git hook) configuration file. This step will generate a
.husky/commit-msg
file, which also needs to be committed to git. This step is to execute commitlint when hook git commit.npx husky install npx husky add .husky/commit-msg "npx --no -- commitlint --edit ${1}"
-
Modify the
.husky/pre-commit
file automatically generated by husky. During the execution ofhusky install
in the previous step, husky will automatically create this file for users to modify by themselves. The default is to executenpm test
. For non-npm-managed projects, In other words, there is no need to execute this command, you can delete this file directly, or you can perform any operation you want to perform pre commit, such as performing a unit test. For example, change to the following:#!/bin/sh . "$(dirname "$0")/_/husky.sh" git fetch origin --prune --prune-tags # Pull Origin's latest submission and prune local redundant tags python3 ./run.py # Execute the build script, if the build fails (such as sys.exit(1) in the middle), the commit will be aborted
-
Add a hook to install husky in
package.json
. It is recommended to addhusky install
in thescripts
field of your project root package json, so that when someone executesnpm install
, the husky hook will be installed automatically.// package.json { "scripts": { "prepare": "husky install" } }
-
Modify the configuration of commitlint to the Gitmoji specification mentioned above. Add a
.commitlintrc.js
file in the root directory of the project with the following content. You can copy the file from this repository to your repository directory and submit it to git.// The following emoji array is obtained from this json, // you may need to add the latest emoji to this array yourself // https://github.com/carloscuesta/gitmoji/blob/master/src/data/gitmojis.json const gitmojis = [ '🎨','⚡️','🔥','🐛','🚑','✨','📝','🚀','💄','🎉', '✅','🔒','🔖','🚨','🚧','💚','⬇️','⬆️','📌','👷', '📈','♻️','➕','➖','🔧','🔨','🌐','✏️','💩','⏪', '🔀','📦','👽','🚚','📄','💥','🍱','♿️','💡','🍻', '💬','🗃','🔊','🔇','👥','🚸','🏗','📱','🤡','🥚', '🙈','📸','⚗','🔍','🏷️','🌱','🚩','🥅','💫','🗑', '🛂','🩹','🧐','⚰️' ]; module.exports = { // Default rule: config-conventional // https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional#rules extends: ['@commitlint/config-conventional'], parserPreset: { parserOpts: { // We change the parser regex pattern to match emoji UTF-8 character // The "[\u23ea-\ufe0f]{1,2}" means that some emojis are in two bytes but not one // So this pattern matchs string like "🐛 Fix a bug" headerPattern: /^([\u23ea-\ufe0f]{1,2})(?:\(([\w\$\.\-\* ]*)\))? (.*)$/, headerCorrespondence: ['type', 'scope', 'subject'] } }, rules: { 'type-enum': [2, 'always', gitmojis], 'type-case': [0, 'always', 'lower-case'], 'subject-full-stop': [2, 'never', '.'], 'subject-case': [2, 'always', 'sentence-case'], 'header-min-length': [2, 'always', 15], 'header-max-length': [2, 'always', 100], 'body-max-line-length': [2, 'always', 200], } };
-
After the above steps are completed and committed && pushed to git remote, it is recommended to add a script to detect the
commitlint
node module in the build script of the project or other places where developers must pass (such as Xcode's PreBuild RunScript Build Phases, etc.) to make sure that all members of the project team havecommitlint
installed. This repository provides a simple script that can be hung in your build script. For details, please refer to./run.py
-
After completion, you can self-test whether it is effective. When the committed message does not meet the specifications, the operation will be aborted and an error message will be displayed, for example:
➜ gitmoji_commitlint_guide git:(master) ✗ git commit -m "📝 Update" ⧗ input: 📝 Update ✖ header must not be shorter than 15 characters, current length is 9 [header-min-length] ✖ found 1 problems, 0 warnings ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint husky - commit-msg hook exited with code 1 (error) ➜ gitmoji_commitlint_guide git:(master) ✗ ➜ gitmoji_commitlint_guide git:(master) ✗ git commit -m "Update README.md" ⧗ input: Update README.md ✖ subject may not be empty [subject-empty] ✖ type may not be empty [type-empty] ✖ found 2 problems, 0 warnings ⓘ Get help: https://github.com/conventional-changelog/commitlint/#what-is-commitlint husky - commit-msg hook exited with code 1 (error) ➜ gitmoji_commitlint_guide git:(master) ✗ ➜ gitmoji_commitlint_guide git:(master) ✗ git commit -m "📝 Update README.md" [master 8a186ca] 📝 Update README.md 1 file changed, 1 insertion(+) ➜ gitmoji_commitlint_guide git:(master) ✗