在一些(非常)罕见的情况下,我在我的存储库中进行了一些更改,这些更改是如此不言自明,以至于描述我的意图的提交消息有些无用.在这些情况下,我希望提交消息基本上只列出我添加/删除/编辑的文件.例如:
添加'dog.h','cat.h'
手动提交消息看起来像
添加了头文件
在这种情况下,不必实际编写提交消息,而是自动生成提交消息会很好.
我知道这是非常糟糕的做法,但我只会将此用于私人项目的非专业存储库.我知道这很懒,但我很好奇它是如何做到的.Unix shell脚本是首选,但欢迎使用任何解决方案.
问:有没有办法自动生成git提交消息,列出已更改的文件?
如果你真的很懒,你可以使用以下内容.简而言之,它执行a git status,为new files,提取行deleted,renamed和modified,并将其传递给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 -
Run Code Online (Sandbox Code Playgroud)
调整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
Run Code Online (Sandbox Code Playgroud)
编辑:更改原始内容grep以sed通过在Changes to be committed和之间包含行来使提交消息生成逻辑更通用Untracked 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>
Run Code Online (Sandbox Code Playgroud)
现在您只需#从要插入的行中删除(例如修改后的行)。
我真的不鼓励你这样做,提交消息非常重要。
具有不同(也许更好)方法的相关问题。
下面是 @adrian-shum 命令的变体,它git status --porcelain更加强大,格式为复制粘贴:可 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
Run Code Online (Sandbox Code Playgroud)
为什么要这么复杂的替换呢?因为修饰符可以分组,例如“RM a -> b”意味着文件被重命名和修改。
| 归档时间: |
|
| 查看次数: |
7439 次 |
| 最近记录: |