如何自动生成提交消息



在某些(非常)罕见的情况下,我会在我的存储库中进行一些不言自明的更改,以至于描述我意图的提交消息有些无用。在这些情况下,我希望提交消息基本上只列出我添加/删除/编辑的文件。例如:

添加了"dog.h"、"cat.h"

手动提交消息如下所示

添加了头文件

在这种情况下,最好

不必实际编写提交消息,而是自动生成它。

我知道这是非常糟糕的做法,但我只会将其用于用于私人项目的非专业存储库。我知道这很懒惰,但我很好奇它是如何做到的。Unix shell脚本是首选,但欢迎任何解决方案。

问:有没有办法自动生成 git 提交消息,列出已更改的文件?

如果你真的那么懒,你可以只使用以下内容。 简而言之,它做一个git status,提取new filesdeletedrenamedmodified的行,并将其传递给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

来自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,通过在 Changes to be committedUntracked files 之间包含行来使提交消息生成逻辑更加通用,并生成外观稍好看的提交消息)

如果您

不提供消息(使用 -m 标志),将打开一条自动生成的消息并要求您修改它(如果有)。它看起来像:

# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
# On branch <branch>
# Changes to be committed:
#       deleted:    <deleted files>
#       modified:   <modified files>
#
# Untracked files:
#       <untracked files>

现在,您只需从要插入的行中删除#(例如修改后的行)。

我真的不鼓励你这样做,提交消息非常重要。

具有不同(也许更好)方法的相关问题。

您可以使用

commit -m 命令将任何消息作为提交消息传递。

<小时 />

另一种解决方案是使用模板配置选项来定义默认模板。

commit.template

存在commit.template配置变量。

提交模板

指定要用作新提交消息模板的文件。
"~/"扩展为$HOME的值,"~user/"扩展到指定用户的主目录。

这是 @adrian-shum 命令的变体,它使用 git status --porcelain 更健壮,格式为下面的复制粘贴:able git 别名:

Git 别名保留在此要点中:
https://gist.github.com/erikw/654386d35ecfdb0354cd2b71763f19ae

这将生成一个带有消息(最后一行)的提交,例如:

$ git status --porcelain
A file1.py
A file2.py
A file3.py
M file4.py
M file5.py
D README.md
R test.txt-> test2.txt
$ git commit-status
$ git log --no-decorate -n 1
bee4f8e Added: file1.py file2.py file3.py Modified: file4.py file5.py Deleted: README.md Renamed: test.txt-> test2.txt

为什么如此复杂的替换?因为修饰符可以分组,例如"RM a -> b"意味着文件既被重命名又被修改。

在这种罕见的情况下,我喜欢使用标准的单字消息,例如"琐碎"。 但其他人可能是"无趣"或"添加文件"。 重要的部分可能是它是一致且可识别的。

但我最近开始使用像"Add"和"Doc"这样的git领导者。 因此,这些信息更有意义;例如,"添加:文件"。

如果您正在寻找一种有趣的方式,并且您的团队知道荒谬的消息意味着这是一个微不足道的提交,请使用 http://whatthecommit.com/自动生成幽默的消息。

希望这有帮助,您可以在 .bashrc 中配置别名,在 ls 的别名部分下,如下所示

alias gitit="git commit -pm '`git status -s` Edit# `git log | grep commit | wc -l`'; git push"

这样做的目的是

  1. 显示您所做的更改,并提示您在提交中暂存这些更改 (-p)
  2. 提交一条消息 (-m),其中包含使用 git status -s 所做的更改以及自分支启动以来的更改数 git log | grep commit | wc -l '
  3. 推动更改

示例运行如下

tr@tr-work:~/Gits/devops-ansible-roles$ gitit
diff --git a/README.md b/README.md
index 0ba82f15..dd8be086 100755
--- a/README.md
+++ b/README.md
@@ -3,4 +3,7 @@
 ==============================================================
 This repository is a collection of Ansible Roles and associated artifacts for executing those roles such as scripts, templates and variable files.
-These roles are called by Jenkins Pipelines defined in the devops-jenkins-pipelines repo.
 No newline at end of file
+These roles are called by Jenkins Pipelines defined in the devops-jenkins-pipelines repo.
+
+
+//
(1/1) Stage this hunk [y,n,q,a,d,e,?]? y
[feature/DO-389_manage_dbs c6dfacf8]  M README.md Edit# 184
 1 file changed, 4 insertions(+), 1 deletion(-)
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 314 bytes | 314.00 KiB/s, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: 
remote: Create pull request for feature/DO-389_manage_dbs:
remote:   https://bitbucket.org/retracted/devops-ansible-roles/pull-requests/new?source=feature/DO-389_manage_dbs&t=1
remote: 
To bitbucket.org:retracted/devops-ansible-roles
   4e14490e..c6dfacf8  feature/DO-389_manage_dbs -> feature/DO-389_manage_dbs
tr@tr-work:~/Gits/devops-ansible-roles$ git log
commit c6dfacf825e6c325bb579de29305d82a7b6bd07d (HEAD -> feature/DO-389_manage_dbs, origin/feature/DO-389_manage_dbs)
Author: Tanveer Roowala <tanveer.roowala@retracted.com>
Date:   Sat May 16 13:58:31 2020 +1000
     M README.md Edit# 184

我所做的是在保持编辑器窗口打开的同时保存文件,然后并行使用 sed 脚本进行编辑。例如,您可以使用 sed -n '/new/s/#//gp' commit_file,当您满意时,切换到字符串上仅以//g 结尾的 sed -i。然后重新读取文件,如果您使用的是 nano,请按 CTRL+R commit_file,如果您想进行一些手动编辑并退出

最新更新