[英]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:相关的在线文档项目是:
https://git-scm.com/docs/git-commit#Documentation/git-commit.txt--Cltcommitgt https://git-scm.com/docs/git-commit#Documentation/git-commit.txt--Cltcommitgt
-C -C
--reuse-message=<commit> --reuse-message=<提交>Take an existing commit object, and reuse the log message and the authorship information (including the timestamp) when creating the commit.获取现有的提交对象,并在创建提交时重用日志消息和作者信息(包括时间戳)。
https://git-scm.com/docs/git-commit#Documentation/git-commit.txt--e https://git-scm.com/docs/git-commit#Documentation/git-commit.txt--e
-e -e
--edit - 编辑The message taken from file with -F , command line with -m , and from commit object with -C are usually used as the commit log message unmodified.从带有-F 的文件、带有-m 的命令行以及带有-C 的提交对象中获取的消息通常用作未修改的提交日志消息。 This option lets you further edit the message taken from these sources.此选项可让您进一步编辑从这些来源获取的消息。
and as suggested by @KevinReid:正如@KevinReid 所建议的:
https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---reset-author https://git-scm.com/docs/git-commit#Documentation/git-commit.txt---reset-author
--reset-author --重置作者
When used with -C/-c/--amend options, or when committing after a conflicting cherry-pick, declare that the authorship of the resulting commit now belongs to the committer.当与-C/-c/--amend选项一起使用时,或者在发生冲突的cherry-pick 之后提交时,声明结果提交的作者现在属于提交者。 This also renews the author timestamp.这也会更新作者时间戳。
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.