如何删除 git 作者提交历史?

How to erase git author commit history?

我有一个内部 gitlab 存储库。

存储库托管许多项目。

其中一些项目是定制的外部开源项目。

我希望提交历史记录仅反映内部完成的提交。

那么我怎样才能删除原作者的名字和他们的提交,这样它就不会出现在 gitlab 的分析历史中。

修改 git 历史记录的最简单方法之一是 filter branch

使用类似的东西,并根据您的用途进行调整:

git filter-branch --env-filter '
    if test "$GIT_AUTHOR_EMAIL" = "SPECIFIC_AUTHOR_EMAIL"
    then
        GIT_AUTHOR_EMAIL=nobody
        export GIT_AUTHOR_EMAIL
    fi
' -- --all

不要忘记之前备份您的存储库,并且您必须使用 push --force 因为它会更改提交 ID。

要删除旧提交,请使用 git checkout --orphan

示例:

提交历史:

* 5 History you want to keep
* 4 History you want to keep
* 3 First commit you want to keep
* 2 Commit you want to delete
* 1 Commit you want to delete

Git 命令:

git checkout --orphan new_root 2
git commit -m'truncated history'
git rebase --onto new_root 2 master
git branch -D new_root

将 2 替换为您要删除的最后一次提交的哈希值。

您必须使用 git push origin master --force 来更新服务器上的代码。所有提交的哈希值都将更新,因此您拥有的任何功能分支都需要更新。

我刚刚删除了 .git 文件并做了一个新的 git 初始化。

此解决方案存在的问题是:

  1. 它删除了所有 git 提交,其中包括我的团队所做的一些提交。

  2. 你失去了所有你可以从中学习的原始历史。

最好留个备份。