如何自动`git commit amend` to*append*to last commit message?

Ome*_*r H 4 git

尝试通过在上一条消息的末尾附加一个字符串来编辑上次提交消息.

尝试从CI服务器执行此操作,因此我正在寻找一种不需要任何交互式人工干预的自动解决方案

Edw*_*son 8

如果要修改当前提交并更改提交消息而不打开编辑器,可以使用该-m标志提供消息.例如:

git commit --amend -m"This is the commit message."
Run Code Online (Sandbox Code Playgroud)

将使用作为新消息给出的消息修改当前HEAD:

% git log
commit adecdcf09517fc4b709fc95ad4a83621a3381a45
Author: Edward Thomson <ethomson@edwardthomson.com>
Date:   Fri Mar 17 12:29:10 2017 +0000

    This is the commit message.
Run Code Online (Sandbox Code Playgroud)

如果要附加消息,则需要获取前一个消息:

OLD_MSG=$(git log --format=%B -n1)
Run Code Online (Sandbox Code Playgroud)

然后您可以使用它来设置新消息,使用多个-m来设置多行:

git commit --amend -m"$OLD_MSG" -m"This is appended."
Run Code Online (Sandbox Code Playgroud)

这会将给定的消息附加到原始消息:

% git log
commit 5888ef05e73787f1f1d06e8f0f943199a76b70fd
Author: Edward Thomson <ethomson@edwardthomson.com>
Date:   Fri Mar 17 12:29:10 2017 +0000

    This is the commit message.

    This is appended.
Run Code Online (Sandbox Code Playgroud)


Sam*_*iar 7

一种方法是使用GIT_EDITOR环境变量并修改提交:

GIT_EDITOR="echo 'appended line' >>" git commit --amend
Run Code Online (Sandbox Code Playgroud)

哪里appended line是你要插入的内容。

该变量将设置用于此提交操作的编辑器。基本上,Git 将执行该变量设置的任何内容,传递包含提交消息的文件名,即$GIT_EDITOR <file>,在本例中为echo 'appended line' >> <file>.