编辑数百条Git提交消息的最快方法是什么?

Wal*_*t D 27 git

我有一个相当大的Git存储库,有1000个提交,最初是从SVN导入的.在我公开回购之前,我想清理一些在我的新回购中没有意义的提交消息,以及删除所有添加的git-svn信息文本.

我知道我可以使用'git rebase -i'然后'git commit --amend'来编辑每个单独的提交消息,但是要编辑数百条消息,这对你知道什么是一个巨大的痛苦.

有没有更快的方法来编辑所有这些提交消息?理想情况下,我将每个提交消息列在一个文件中,我可以在一个地方编辑它们.

谢谢!

Lex*_*tor 24

这是一个老问题,但由于没有提及git filter-branch,我只是加上我的两分钱.

我最近不得不大规模替换文本在提交信息,由另一个替换文本块不改变提交信息的其余部分.举例来说,我不得不更换参考文献:#xxxxx参考文献:#22917.

我用git filter-branch这样的

git filter-branch --msg-filter 'sed "s/Refs: #xxxxx/Refs: #22917/g"' master..my_branch
Run Code Online (Sandbox Code Playgroud)
  • 我使用该选项--msg-filter仅编辑提交消息,但您可以使用其他过滤器来更改文件,编辑完整提交信息等.
  • filter-branch通过仅将其应用于不在master(master..my_branch)中的提交来限制,但是您可以通过省略提交范围将其应用于整个分支.

正如文档中所建议的那样,在您的分支副本上尝试此操作.希望有所帮助.


用于答案的来源

  • --msg-filter选项在STDOUT上输出提交消息并在STDIN上接收新的提交消息,因此您需要使用"sed"而不是"sed -i":<pre> <code> git filter-branch --msg-filter' sed"s/Refs:#xxxxx/Refs:#22917/g"'master..my_branch </ code> </ pre> (2认同)
  • @SimonDesfarges你是对的!它不起作用,我马上更新我的答案 (2认同)

mvp*_*mvp 13

这很容易做到如下:

  • 执行首次导入.
  • 将所有提交导出到文本中:

    git format-patch -10000
    
    Run Code Online (Sandbox Code Playgroud)

    数字应该超过总提交次数.这将创建许多名为的文件NNNNN-commit-description.patch.

  • 使用某些脚本编辑这些文件.(除了带有提交消息的top之外,不要触摸它们中的任何内容).
  • 将已编辑的文件复制或移动到空git仓库或分支.
  • 导入所有已编辑的提交:

    git am *.patch
    
    Run Code Online (Sandbox Code Playgroud)

这只适用于单个分支,但效果很好.

  • 使用"git am --committer-date-is-author-date"来保留提交日期 (4认同)

Ste*_*hen 7

现在推荐git-filter-repo https://github.com/newren/git-filter-repo。我像这样使用它:

PS C:\repository> git filter-repo --commit-callback '
>> msg = commit.message.decode(\"utf-8\")
>> newmsg = msg.replace(\"old string\", \"new string\")
>> commit.message = newmsg.encode(\"utf-8\")
>> ' --force
New history written in 328.30 seconds; now repacking/cleaning...
Repacking your repo and cleaning out old unneeded objects
HEAD is now at 087f91945a blah blah
Enumerating objects: 346091, done.
Counting objects: 100% (346091/346091), done.
Delta compression using up to 8 threads
Compressing objects: 100% (82068/82068), done.
Writing objects: 100% (346091/346091), done.
Total 346091 (delta 259364), reused 346030 (delta 259303), pack-reused 0
Completely finished after 443.37 seconds.
PS C:\repository>
Run Code Online (Sandbox Code Playgroud)

你可能不想复制 powershell 额外的东西,所以这里只是命令:

git filter-repo --commit-callback '
msg = commit.message.decode(\"utf-8\")
newmsg = msg.replace(\"old string\", \"new string\")
commit.message = newmsg.encode(\"utf-8\")
' --force
Run Code Online (Sandbox Code Playgroud)

如果你想击中所有的分支,不要使用--refs HEAD. 如果您不想使用--force,可以在干净的git clone --no-checkout. 这让我开始了:https : //blog.kawzeg.com/2019/12/19/git-filter-repo.html


maa*_*nus 6

您可以使用git rebase -i并替换pickreword(或仅替换为r)。然后 git rebase 在每次提交时都会停止,让您有机会编辑消息。

唯一的缺点是您无法立即看到所有消息,并且在发现错误时无法返回。

  • 这不是要走的路。一次打开 1000 条提交消息大约需要 8.5 小时才能完成所有这些消息。计算一条消息大约30秒 (2认同)