如何自动生成提交消息 [英] How to automatically generate commit message
问题描述
在某些(非常)罕见的情况下,我对存储库进行了一些不言自明的更改,以至于描述我的意图的提交消息有些用处.在这些情况下,我希望提交消息基本上只是列出我添加/删除/编辑的文件.例如:
In some (very) rare occasions, I make some changes in my repository that are so self-explanatory that a commit message describing my intentions is somewhat useless. In these cases, I would like the commit message to basically just list what files I've added/removed/edited. For instance:
添加了"dog.h","cat.h"
Added 'dog.h', 'cat.h'
手动提交消息看起来像
添加了头文件
在这种情况下,不必实际编写提交消息,而是自动生成提交消息,将是很好的选择.
In situations like this it would be nice to not have to actually write the commit message, but rather have it automatically generated.
我知道这是非常糟糕的做法,但是我只会将其用于私有项目的非专业存储库.我知道这很懒,但是我很好奇它是如何完成的.首选使用Unix shell脚本,但是欢迎任何解决方案.
I'm aware that this is very bad practice, but I would only use this for non-professional repositories used for private projects. I know it's lazy, but I'm curious as to how it can be done. Unix shell scripts are preferred, but any solution is welcome.
问:是否可以自动生成git commit消息,列出已更改的文件?
Q: Is there a way to automatically generate a git commit message, listing what files that has been changed?
推荐答案
如果您确实很懒,则可以使用以下内容.简而言之,它会执行 git status
,为新文件
,已删除
,重命名
和修改
,并将其传递给 git commit
If you are really that lazy you may just use the following. In brief, it does a git status
, extract lines for new files
, deleted
, renamed
and modified
, and pass it to git commit
# LANG=C.UTF-8 or any UTF-8 English locale supported by your OS may be used
LANG=C git -c color.status=false status \
| sed -n -r -e '1,/Changes to be committed:/ d' \
-e '1,1 d' \
-e '/^Untracked files:/,$ d' \
-e 's/^\s*//' \
-e '/./p' \
| git commit -F -
调整 sed
部分以基于 git status
将其简称为别名,或将其另存为脚本(例如 git-qcommit
),以便将其用作 git qcommit
Alias it to something short, or save it as a script (e.g. git-qcommit
) so that you can use it as git qcommit
来自 git log
adrianshum:~/workspace/foo-git (master) $ git log
commit 78dfe945e8ad6421b4be74cbb8a00deb21477437
Author: adrianshum <foo@bar.com>
Date: Wed Jan 27 01:53:45 2016 +0000
renamed: bar.txt -> bar2.txt
modified: foo.txt
将原始 grep
更改为 sed
,从而通过在要提交的更改
和未跟踪的文件
,并生成外观稍好一点的提交消息)
Edited:
Changed original grep
to sed
to make the commit message generation logic more generic by including lines between Changes to be committed
and Untracked files
, and produce a slightly better looking commit message)
这篇关于如何自动生成提交消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!