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
相关产品推荐
相关产品推荐

