You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用.gitattributes对所有分支执行git lfs migrate迁移?

基于自定义.gitattributes迁移全Git仓库历史到LFS

操作步骤

  1. 准备好你的.gitattributes
    确保工作区里有已配置好的.gitattributes(比如*.zip filter=lfs diff=lfs merge=lfs -text这类规则),不要提交它,保持未暂存状态。

  2. 执行迁移命令
    运行这条命令是关键,它会严格遵循你现有的.gitattributes规则,且不会修改该文件:

    git lfs migrate import --everything --no-rewrite .gitattributes --include="*"
    

    各参数作用:

    • --everything:覆盖所有分支、标签和提交历史
    • --no-rewrite .gitattributes:强制Git LFS使用你本地的配置文件,不自动生成或修改它
    • --include="*":触发全文件扫描,但仅应用.gitattributes里定义的追踪规则,不会乱加无关文件类型
  3. 验证与收尾

    • 检查所有历史中的目标文件是否已被LFS接管:
      git lfs ls-files --all
      
    • 确认.gitattributes未被改动:
      git status
      
    • 提交配置文件并推送重写后的历史(注意:历史重写后,团队成员需要重新克隆或执行git pull --rebase):
      git add .gitattributes
      git commit -m "Configure Git LFS tracking"
      git push --force --all
      git push --force --tags
      

失败原因复盘

  • 使用--fixup:该选项仅修复已存在的LFS追踪异常,不会主动将文件转为LFS,所以无文件被追踪
  • 未加--no-rewrite:Git LFS会自动生成.gitattributes,扫描所有文件并添加类型规则,导致纯文本文件被错误纳入
  • 提前提交.gitattributes:迁移命令会读取历史中的旧版本配置,而非你当前的新规则

内容的提问来源于stack exchange,提问作者Tientuinë

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 08:55:20