使用新的 .gitattributes 文件迁移 Git-LFS

Ada*_*ith 7 git git-lfs

我有一个较大的(32k 次提交)git 存储库,我需要在其中重写一个分支中的历史记录以删除 .gitattributes 文件所描述的一堆大文件。这个分支完全是本地的,从来没有碰到过远程(实际上我们的远程因为历史上的大文件而拒绝了它)。

我知道以下命令将遍历分支的历史记录并删除所有.dll文件:

$ git lfs migrate import --include='*.dll'
Run Code Online (Sandbox Code Playgroud)

但是由于 .gitattributes 文件存在并且相当广泛,如果在创建分支时 .gitattributes 文件已经存在,那么是否有一个命令可以简单地重放为指针化这些文件所做的工作?

LeG*_*GEC 3

.gitattributes我首先会在分支的开头插入正确的内容(git rebase例如使用):

*--*--x--*--*--*--*--* <- master
       \
        *--*--*--*--*--*--a--* <- my/branch
                          ^
                          commit with updated .gitattributes

# with the commits identified as above, from branch my/branch, run :
$ git rebase -i x
   ...
   # in the opened editor, move commit 'a' at the beginning of the list
   # save & close

# you should obtain :
*--*--x--*--*--*--*--* <- master
       \
        a'--*--*--*--*--*--*--* <- my/branch (rewritten)
        ^
      rewritten commit
Run Code Online (Sandbox Code Playgroud)

在那之后 :

您可以使用git filter-branch --tree-filtergit 来一次又一次地重播提交,并应用以下描述的过滤器.gitattributes

# first arg is the name of a script to execute on each commit
#   you have nothing to edit : just use 'true' as an action
#   the only action you expect is that git applies the filters
#   when re-staging files for each individual commit

git filter-branch --tree-filter true a'..my/branch
Run Code Online (Sandbox Code Playgroud)

您可能想要添加该--prune-empty选项,或者您可以在重写后删除空提交,git rebase -i例如再次使用。