如何从 git 存储库中删除作者?
如果我创建一个 Git 存储库并公开发布它(例如在 GitHub 等上),并且我收到存储库贡献者的请求,无论出于何种原因删除或隐藏他们的名字,有没有一种方法可以轻松做到这一点?
基本上,我有过这样的请求,可能希望将他们的姓名和电子邮件地址替换为“匿名贡献者”之类的内容,或者可能是他们电子邮件地址的 SHA-1 哈希值或类似的内容。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Jeff 说得很对,正确的路线是 git filter-branch。它需要一个处理环境变量的脚本。对于您的用例,您可能想要这样的东西:
您可以测试它是否像这样工作:
Jeff is quite right, the right track is git filter-branch. It expects a script that plays with the environment variables. For your use case, you probably want something like this:
You can test that it works like this:
Please beware. There's no way to delete the author's name without invalidating all later commit hashes. That will make later merging a pain for people that have been using your repository.
如果您不仅需要对某个用户,而且还需要对所有用户“匿名”git 存储库,Git 2.2(2014 年 11 月)提供了一项有趣的功能,其中改进和增强了
git fast-export
:参见 提交 a872275 和 提交 75d3d65 by Jeff King (
peff
):教学
快速导出
一个--anonymize
选项:文档:
另请参阅 Git 2.28(2020 年第 3 季度),“
git fast-export --anonymize
” 学会了采用自定义映射,以允许用户调整其输出,使其更可用于调试。请参阅提交f39ad38、提交 8a49495, 提交 d5bf91f,提交 6416a86,提交 55b0145, 提交 a0f6564、提交 7f40759、提交 750bb32, 提交 b897bf5,提交 b8c0689(2020 年 6 月 23 日),作者:杰夫·金 (
peff
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 0a23331,2020 年 7 月 6 日)git fast-export< /code>
现在有:
在 Git 2.34(2021 年第 4 季度)之前,“ 的输出
git fast-export
"( man),在使用其匿名功能时,错误地显示了带注释的标签。请参阅 提交 2f040a9(2021 年 8 月 31 日),作者:Tal Kelrich (
hasturkun
)。(由 Junio C Hamano --
gitster
-- 合并于 提交 febba80,2021 年 9 月 10 日)If you ever have to "anonymize" a git repo not just for one user, but all users, Git 2.2 (November 2014) provides an interesting feature with the improved and enhanced
git fast-export
:See commit a872275 and commit 75d3d65 by Jeff King (
peff
):teach
fast-export
an--anonymize
option:Doc:
See also Git 2.28 (Q3 2020), "
git fast-export --anonymize
" learned to take customized mapping to allow its users to tweak its output more usable for debugging.See commit f39ad38, commit 8a49495, commit 65b5d9f (25 Jun 2020), and commit d5bf91f, commit 6416a86, commit 55b0145, commit a0f6564, commit 7f40759, commit 750bb32, commit b897bf5, commit b8c0689 (23 Jun 2020) by Jeff King (
peff
).(Merged by Junio C Hamano --
gitster
-- in commit 0a23331, 06 Jul 2020)git fast-export
now have:Before Git 2.34 (Q4 2021), the output from "
git fast-export
"(man), when its anonymization feature is in use, showed an annotated tag incorrectly.See commit 2f040a9 (31 Aug 2021) by Tal Kelrich (
hasturkun
).(Merged by Junio C Hamano --
gitster
-- in commit febba80, 10 Sep 2021)您可以在本地存储库中进行更改,
git commit --amend
适当的提交(您添加名称的位置),然后git push --force
更新 github与您的存储库版本。带有贡献者姓名的原始提交仍然可以在引用日志中找到(直到它过期,但是需要花费很多精力才能找到它。如果这是一个问题,您也可以从引用日志中删除该特定提交 - 请参阅
git help reflog
了解语法以及如何在列表中找到它。You can make the change in your local repository,
git commit --amend
the appropriate commit (where you added the name), and thengit push --force
to update github with your version of the repository.The original commit with the contributor's name will still be available in the reflog (until it expires, but it would take a lot of effort to find it. If this is a concern, you can obliterate that specific commit from the reflog too -- see
git help reflog
for the syntax and how to find it in the list.如果您想更改多个提交,请查看手册页
您可以使用 git-filter-branch 更改先前提交的内容/元。
请注意,由于您不处理本地分支(它已被推送到 github),因此您无法从任何已克隆您分支的人中删除作者。
修改已经发布的分支通常也是不好的做法,因为这可能会导致跟踪分支的人感到困惑。
If you want to change more than one commit, check out the man page for
You can use git-filter-branch to change the content/meta of previous commits.
Note that since you're not dealing with a local branch (it's already been pushed to github), you have no way to remove the author from anyone who has already cloned your branch.
It's also generally bad practice to modify a branch which has already been published, since it can lead to confusion for people who are tracking the branch.