使用 git filter-branch 重写作者/提交者并同时提交消息
我有一个最初从 Subversion 导入的 Git 存储库。部分作者/提交者信息是错误的,这不是 Git 的错,而主要是由于 Subversion 提交的草率造成的。
我想使用 git filter-branch
来重写存储库的历史记录,修复提交者和作者信息。
问题是...我需要从提交消息中获取作者信息。据我所知, git filter-branch 允许您过滤和更改作者信息(使用 --env-filter
)和/或过滤提交消息(使用 --msg-filter
),但不能同时执行这两项操作,并在不同过滤器之间共享信息。
所以我对如何做到这一点感到困惑......我能想到的最好的方法是分多次进行:首先,收集所有提交消息,然后制作一个脚本来遍历并过滤所有作者/提交者信息。这看起来相当不优雅且容易出错,所以我想知道是否有其他人已经找到了更顺利地完成此类工作的方法。
I have a Git repository originally imported from Subversion. Parts of the author/committer information are wrong, which is not Git's fault but mostly due to sloppy committing with Subversion.
I would like to use git filter-branch
to rewrite the history of the repository, fixing the committer and author information.
The trouble is... I need to slurp author information out of the commit messages. As far as I can tell, git filter-branch
allows you to filter and alter the author information (with --env-filter
) and/or to filter the commit messages (with --msg-filter
), but not to do both simultaneously, with information shared between the different filters.
So I'm kind of stumped about how to do this... the best I can think of is to do it in multiple passes: first, collect allllll the commit messages, then make a script to go through and filter all the author/committer info. This seems quite inelegant and error-prone, so I'm wondering if anyone else has figured out a do this kind of work more smoothly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我唯一能想到的一次性完成的事情就是使用提交过滤器。与消息过滤器一样,它获取标准输入上的日志消息,因此您将能够解析它并找出您需要的内容。然后,您可以自己设置适当的变量并自己调用 git commit-tree 。 (提交过滤器本质上是提交树的直接替代品,采用相同的参数并产生相同的输出。)
在 bash 中,它会是这样的:
我从未尝试过这个,但我不能假设您正确编写了这两个 shell 函数,看看为什么它不起作用!
(只是一个小注释 - 并不是说
--env-filter
和--msg-filter
不能互相影响,而是它们总是因此,第一个过滤器可能会在文件或环境中留下副作用,供其他过滤器看到,但它们的顺序会阻止您执行您想要的操作。)The only thing I can think of to get it done in one pass is to use a commit filter. Like the message filter, it takes the log message on stdin, so you will be able to parse it and find out what you need to. You can then set the appropriate variables yourself and call
git commit-tree
yourself. (The commit filter is essentially a drop-in replacement for commit-tree, taking the same arguments and producing the same output.)In bash, it'd be something like this:
I've never tried this, but I can't see why it wouldn't work, assuming you write those two shell functions properly!
(And just a small note - it's not so much that
--env-filter
and--msg-filter
can't influence each other, it's that they're always run in that order. So, the first filter could leave behind side-effects in files or environment for the other to see, but they're in an order that keeps you from doing what you want.)