简体   繁体   English

使用父提交的文本开始 git commit 的文本

[英]Start text of git commit with the text of the parent commit

When you create a new commit in git, you can specify that you want your commit message editor to start up with the contents of a file, by specifying git commit -t <filename> or the commit.template environment variable.当您在 git 中创建新提交时,您可以通过指定git commit -t <filename>commit.template环境变量来指定您希望提交消息编辑器以文件内容启动。 Is there a way to specify that you always want to use the full text of the parent commit as your template?有没有办法指定您始终希望使用父提交的全文作为模板?

Context: My usual codebase, like many others, requires an annotation of what bug/issue a commit is work on to be present in every commit.上下文:与许多其他代码库一样,我通常的代码库需要对提交所处理的错误/问题进行注释,以便在每次提交中出现。 Remembering bug/issue numbers is hard, and frequently I have a stack of commits on the same topic, to be submitted in sequence.记住错误/问题编号很困难,而且我经常有一堆关于同一主题的提交,要按顺序提交。 It's awkward to use incantations like git commit; Ctrl+Z; g log -1; <click>; Shift+Ctrl+C; fg; Shift+Ctrl+V使用像git commit; Ctrl+Z; g log -1; <click>; Shift+Ctrl+C; fg; Shift+Ctrl+V这样的咒语很尴尬git commit; Ctrl+Z; g log -1; <click>; Shift+Ctrl+C; fg; Shift+Ctrl+V git commit; Ctrl+Z; g log -1; <click>; Shift+Ctrl+C; fg; Shift+Ctrl+V git commit; Ctrl+Z; g log -1; <click>; Shift+Ctrl+C; fg; Shift+Ctrl+V , which is what I'm usually forced to do. git commit; Ctrl+Z; g log -1; <click>; Shift+Ctrl+C; fg; Shift+Ctrl+V ,这是我通常被迫做的。

You should just need to run:你应该只需要运行:

git commit -C HEAD --reset-author -e

or more generally:或更一般地说:

git commit -C $TEMPLATE_COMMIT_SHA1 --reset-author -e

The relevant online documentation items are:相关的在线文档项目是:

If you want to make this command more practical, eg by relying on an env.如果你想让这个命令更实用,例如通过依赖一个 env。 variable TEMPLATE_COMMIT_SHA1 , you can define a git alias such as:变量TEMPLATE_COMMIT_SHA1 ,您可以定义一个 git 别名,例如:

# "committ" stands for "commit-template"
git config alias.committ '!f(){ set -x; git commit -C "${TEMPLATE_COMMIT_SHA1:-HEAD}" --reset-author -e "$@"; }; f'

# or if you prefer to make this alias available for all repos:
git config --global alias.committ '!f(){ set -x; git commit -C "${TEMPLATE_COMMIT_SHA1:-HEAD}" --reset-author -e "$@"; }; f'

# demo
touch a; git add a; git committ

export TEMPLATE_COMMIT_SHA1=HEAD^^^
touch b; git add b; git committ

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM