git commit一直没有一个规范,但是经常翻阅大型仓库例如linux kernel的都能感觉到commit是有一定规范的。为了让自己提交代码更合规点,这里整理了规范和一些注意事项(不一定正确哦),如下分享
Module: SubModule: Brief description of the patch ################################################################################# # Reference: https://www.kernel.org/doc/html/v5.4/process/submitting-patches.html # Notice: # 1. Only one problem was solved. # 2. Full description and justification # 3. Only one problem per patch # # Example 1: # Module: SubModule: Brief description of the patch # # Example 2: # Module: SubModule: Brief description of the patch # # Detailed description (if desired) # # Example 3: # [PATCH] Foo: Fix these things # or # [PATCH v2] Foo: Fix these things better # or # [PATCH v3 0/2] comedi: Fix these things # [PATCH v3 1/2] comedi: Fix the first thing # [PATCH v3 2/2] comedi: Fix the second thing #################################################################################
上述是.gitmessage文件,这个文件需要在自己的目录~/.gitmessage
下
为了使得这个模板生效,可以git config --global commit.template ~/.gitmessage
这样,以后提交就可以按照这个模板了。
vim ~/.vimrc
filetype plugin indent on syntax on set title set tabstop=8 set softtabstop=8 set shiftwidth=8 set noexpandtab
命令如下
:set tabstop=8 softtabstop=8 shiftwidth=8 noexpandtab
update-alternatives --config editor
apt install esmtp touch ~/.esmtprc chmod g-rwx ~/.esmtprc chmod o-rwx ~/.esmtprc vim ~/.esmtprc identity "tangfengweny@gmail.com" hostname smtp.gmail.com:587 username "tangfengweny@gmail.com" password "邮箱密码" starttls required set sendmail="/usr/bin/esmtp" set envelope_from=yes set from="tangfeng <tangfengweny@gmail.com>" set use_from=yes set edit_headers=yes
vim ~/.gitconfig [user] name = tangfeng email = tangfengweny@example.com
git fetch origin git rebase origin/yourbranch
apt-get install codespell python-ply python-git vim .git/hooks/post-commit #!/bin/sh exec git show --format=email HEAD | ./scripts/checkpatch.pl --strict --codespell chmod a+x .git/hooks/post-commit
这样提交时就默认检查补丁规范了./scripts/checkpatch.pl --strict --codespell
确保提交的commit信息用 git log --oneline
能够清晰了解补丁内容
了解对应提交的maintainer
git show HEAD | perl scripts/get_maintainer.pl --separator , -- nokeywords --nogit --nogit-fallback --norolestats
如果是特定文件
perl scripts/get_maintainer.pl --separator , --nokeywords --nogit -- nogit-fallback --norolestats -f drivers/staging/vt6655/baseband.c
不要直接把补丁发送到邮件列表,要先制作一封特殊的邮件。如下
git format-patch -o /tmp/ HEAD^
先发给自己
mutt -H /tmp/0001-*.patch
也可以git发送
vim .gitconfig [sendemail] smtpserver = /usr/bin/esmtp git send-email --annotate HEAD^
git format-patch --subject-prefix="PATCH v2"
如果需要对补丁额外修改,应该在补丁下添加---,然后开始解释,git工具会忽略---下面的内容
然后在补充内容后面加剪刀符>8-----8<
这样文本会更加清晰
如下:
Signed-off-by: Your Name <my.email@gmail.com> --- Changes since v2: * Made commit message more clear * Corrected grammer in code comment * Used new API instead of depreciated API >8------------------------------------------------------8< drivers/staging/csr/bh.c
这里---下面的是补充的内容
补丁集也就是对所有提交补丁的汇总,如下
git format-patch -n --subject-prefix="PATCH vY" --cover-letter
最后发送补丁集
git send-email --to <发给谁> --cc <addresses from get_maintainer.pl output> /tmp/*.patch
可以先--dry-run
在本地实验后在发送
git send-email --to <发给谁> --cc <addresses from get_maintainer.pl output> -dry-run /tmp/*.patch
https://kernelnewbies.org/FirstKernelPatch
https://kernelnewbies.org/PatchPhilosophy