是否可以在不使用交互模式的情况下运行提交(由哈希标识)的自动改写?我的目标是改写一行,因为我需要从代码中调用。
更新:
我对其进行了一些修改以供内联使用。
git filter-branch --msg-filter "ruby -e\"puts (ENV['GIT_COMMIT'] == '1ba2dd66581f6fbc03d1a6406a0ed61b6473c9ab' ? 'new msg' : STDIN.read)\""HEAD
虽然在纯 bash 中(没有 ruby)会很好,但是我在让 STDIN 工作时遇到了麻烦(使用 read)。
最佳答案
git-filter-branch
可以做你想做的事。具体来说,你想要 --msg-filter
参数:
This is the filter for rewriting the commit messages. The argument is evaluated in the shell with the original commit message on standard input; its standard output is used as the new commit message.
一个问题是 git-filter-branch
想要在分支中的每个 提交上运行,而您只想重写一个 提交(然后有效地重新设置其余部分)。因此,您的消息过滤器需要有一些条件逻辑,以输出新的提交注释(对于您确实想要重写的一次提交)或原始的、未修改的提交消息(对于其他所有内容)。
这就是诀窍:
git filter-branch --msg-filter "ruby /path/to/filter.rb 843c2da5e07de914ea2319fa3295730ff8b3b0a1 'New message'" HEAD
git-filter-branch
在调用您的脚本之前更改当前目录,因此您需要完全限定 /path/to/filter.rb
.
这是 filter.rb
:
if ENV['GIT_COMMIT'] == ARGV[0]
puts ARGV[1]
else
puts STDIN.read
end
这可能可以用 shell 脚本更简洁地重写,也许甚至不需要将消息过滤器脚本放在一个单独的文件中;但我在 Windows 上运行,所以我没有任何花哨的东西。我有 Ruby,所以我使用了它,它确实有效。
关于git - 在没有交互式 rebase 的情况下提交改写?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8782080/