git - 如何在运行 git filter-branch 后删除旧的历史记录?

标签 git git-branch git-filter-branch

假设我有这样一棵树:

... -- a -- b -- c -- d -- ...
             \
              e -- a -- k

我希望它变得公正

... -- a -- b -- c -- d -- ...

我知道如何将分支名称附加到“e”。我知道我要做的事会改变历史,这是不好的。另外我想我需要使用像 rebase 或 filter-branch 这样的东西。但究竟是怎样 - 我迷路了。

好的。情况如下:我现在有相当大的树(像这样)

                 s -- p -- r   
                /
a -- b -- c -- d -- e --- g -- w
           \               \
            t -- p -- l     y -- k

但在我的第一次提交中(例如“b”)我添加了二进制文件,这使得整个 repo 非常沉重。所以我决定把它们带走。我用 filter-branch 做到了。现在,从第二次提交开始,我有 2 个彼此相同的长提交分支。

                 s -- p -- r   
                /
a -- b -- c -- d -- e --- g -- w
      \    \               \
       \    t -- p -- l     y -- k
        \
         \             s'-- p'-- r'  
          \           /
           b'-- c'-- d'-- e'--- g'-- w'
                 \               \
                  t'-- p'-- l'    y'-- k'

其中 b' 是在没有二进制文件的情况下提交的。所以我不能 merge 。我不希望这整棵树在历史上如此重复。

最佳答案

在导入具有多年历史的 Subversion 存储库后,我遇到了类似的问题,即大量二进制 Assets 的膨胀。在 git: shrinking Subversion import ,我描述了将我的 git 存储库从 4.5 GiB 修剪到大约 100 MiB。

假设您想从所有提交中删除 “Delete media files” (6fe87d) 中删除的文件,您可以将我的博客文章中的方法应用到您的存储库中:

$ git filter-branch -d /dev/shm/git --index-filter \
  "git rm --cached -f --ignore-unmatch media/Optika.1.3.?.*; \
   git rm --cached -f --ignore-unmatch media/lens.svg; \
   git rm --cached -f --ignore-unmatch media/lens_simulation.swf; \
   git rm --cached -f --ignore-unmatch media/v.html" \
  --tag-name-filter cat --prune-empty -- --all

您的 github 存储库没有任何标签,但我包含一个标签名称过滤器以防您有私有(private)标签。

git filter-branch documentation涵盖 --prune-empty 选项。

--prune-empty
Some kinds of filters will generate empty commits that leave the tree untouched. This switch allows git-filter-branch to ignore such commits …

使用此选项意味着您重写的历史将不包含“删除媒体文件”提交,因为它不再影响树。媒体文件永远不会在新的历史记录中创建。

此时,由于另一个 documented behavior,您将在存储库中看到重复项.

The original refs, if different from the rewritten ones, will be stored in the namespace refs/original/.

如果您对新改写的历史感到满意,请删除备份副本。

$ git for-each-ref --format="%(refname)" refs/original/ | \
  xargs -n 1 git update-ref -d

Git 对保护你的工作保持警惕,所以即使在所有这些有意重写和删除 reflog 之后使旧的提交保持活力。使用两个命令序列清除它们:

$ git reflog expire --verbose --expire=0 --all
$ git gc --prune=0

现在您的本地存储库已准备就绪,但您需要将更新推送到 GitHub。你可以一次做一个。对于本地分支机构,比如 master,你会运行

$ git push -f origin master

假设您不再有本地 issue5 分支。您的克隆仍然有一个名为 origin/issue5 的引用,用于跟踪它在您的 GitHub 存储库中的位置。运行 git filter-branch 也会修改所有原始引用,因此您可以在没有分支的情况下更新 GitHub。

$ git push -f origin origin/issue5:issue5

如果您所有的本地分支都匹配它们在 GitHub 端的各自提交(,没有未推送的提交),那么您可以执行批量更新。

$ git for-each-ref --format="%(refname)" refs/remotes/origin/ | \
  grep -v 'HEAD$' | perl -pe 's,^refs/remotes/origin/,,' | \
  xargs -n 1 -I '{}' git push -f origin 'refs/remotes/origin/{}:{}'

第一阶段的输出是一个引用名列表:

$ git for-each-ref --format="%(refname)" refs/remotes/origin/
refs/remotes/origin/HEAD
refs/remotes/origin/issue2
refs/remotes/origin/issue3
refs/remotes/origin/issue5
refs/remotes/origin/master
refs/remotes/origin/section_merge
refs/remotes/origin/side-media-icons
refs/remotes/origin/side-pane-splitter
refs/remotes/origin/side-popup
refs/remotes/origin/v2

我们不需要 HEAD 伪引用并使用 grep -v 将其删除。对于其余部分,我们使用 Perl 去除 refs/remotes/origin/ 前缀,并为每个运行形式的命令

$ git push -f origin refs/remotes/origin/BRANCH:BRANCH

关于git - 如何在运行 git filter-branch 后删除旧的历史记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5984428/

相关文章:

git - 解决大型项目团队中的冲突

git - 如何将 ('fork' ) 您自己的个人 GitHub 存储库克隆到单独的新分支中的新存储库?

重命名文件夹后的 Git 过滤

git - 如何在 Git 中分 ionic 目录但保留所有分支

git - 为什么 filter-branch 上的 --cached 选项会从工作目录中删除文件?

Git - 断开的链接、丢失和悬挂的树

spring - 如何防止 Spring Config 将我的本地 git 存储库重置为 origin/master

git - 正确配置下游的 Jenkins GitHub Pull Request Builder

github只能通过VPN访问

git - 追溯地将 Git 提交行视为分支