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

Git LFS误跟踪相似文件名文件的原因及解决方法

Git LFS误跟踪子串文件名的问题

场景

仓库结构:

repo/
  folder/
    abc
    abcdef
  • abc需用Git LFS跟踪,abcdef无需跟踪
  • 在repo目录执行命令:git lfs track --filename "folder/abc"
  • 生成的.gitattributes内容:
    folder/abc filter=lfs diff=lfs merge=lfs -text
    
  • 推送时出现错误:
    LFS upload failed:cts:  50% (1/2), 151 MB | 0 B/s
      (missing) folder/abcdef (<hash>)
    hint: Your push was rejected due to missing or corrupt local objects.
    hint: You can disable this check with: 'git config lfs.allowincompletepush true'
    Uploading LFS objects:  50% (1/2), 151 MB | 0 B/s, done.
    

原因

确实是因为文件名的子串匹配问题。Git LFS的.gitattributes规则默认采用前缀匹配,folder/abc会匹配所有以该路径开头的文件,包括folder/abcdef,导致后者被错误纳入LFS跟踪范围。

解决方法

  • 方法1:添加反向规则排除误跟踪文件
    编辑.gitattributes文件,在原有规则后添加:

    folder/abcdef !filter !diff !merge text
    

    这条规则明确指定folder/abcdef不使用LFS相关处理规则,恢复为普通文件。

  • 方法2:使用精确路径匹配定义跟踪规则
    执行以下命令重新设置跟踪规则:

    git lfs track --filename "^folder/abc$"
    

    生成的.gitattributes会采用正则精确匹配,仅命中folder/abc,不会覆盖folder/abcdef。

  • 后续操作
    修改完成后,重新提交规则和文件:

    git add .gitattributes folder/abc folder/abcdef
    git commit -m "Fix LFS tracking to only target folder/abc"
    git push
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 03:22:52