gpt4 book ai didi

git - 如何批量自动更改git历史中的部分提交消息

转载 作者:行者123 更新时间:2023-12-05 03:11:52 30 4
gpt4 key购买 nike

我们在所有 git 提交前加上票号。有时我需要为一系列提交批量重命名票号。例如,我想在 origin/master大师。所以这个 git 历史:

* JIRA-1111 commit message 5 (master)
* JIRA-1111 commit message 4
* JIRA-42 commit message 3
* JIRA-2222 commit message 2
* JIRA-1111 commit message 1 (origin/master)

将更改为:

* JIRA-2222 commit message 5 (master) # <- renamed from 1111 to 2222
* JIRA-2222 commit message 4 # <- renamed from 1111 to 2222
* JIRA-42 commit message 3
* JIRA-2222 commit message 2
* JIRA-1111 commit message 1 (origin/master)

我知道如何使用 --amend 或交互式 rebase 更改单个提交的提交消息并手动编辑每个提交消息。但我的问题是:

如何在不手动编辑每个提交消息的情况下批量修改一系列提交的提交消息?


(如果您想知道我为什么需要这个:IntelliJ IDEA 中的提交对话框显示了最后的提交消息,其中包含我正在处理的票号。但有时(如果不是所有文件都已提交),它不会' 记住最后一条消息并显示较旧的消息。当这条较旧的消息包含不同的票号时,我们通常会在错误的票号下提交。)

最佳答案

运行 git rebase -i 打开一个编辑器,可以在其中更改提交消息。 Git documentation指出:

By default, Git uses whatever you’ve set as your default text editor ($VISUAL or $EDITOR) or else falls back to the vi editor to create and edit your commit and tag messages. To change that default to something else, you can use the core.editor setting

将编辑器替换为例如更改提交消息的 sed 命令即可解决问题:

FROM=JIRA-1111
TO=JIRA-2222
export EDITOR="sed -e 's/^${FROM}/${TO}/' -e 's/^pick /reword /' -i ''"

第一个 -e 选项适本地更改提交消息,第二个 -epick 关键字更改为 reword-i 强制 sed 在没有备份副本的情况下编辑文件。

导出 EDITOR 变量后,为所需的提交范围运行 git rebase:

git rebase -i origin/master master

编辑:根据@torek 的建议,export EDITOR=... 部分可以更改为

export GIT_SEQUENCE_EDITOR="sed -e 's/^pick /reword /' -i ''"
export EDITOR="sed -e 's/^${FROM}/${TO}/' -i ''"

关于git - 如何批量自动更改git历史中的部分提交消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35971218/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com