【Git】git lfs自动跟踪大文件

  1. 安装Git LFS:git lfs install
  2. 创建文件 detect_large_files.sh :touch detect_large_files.sh
  3. 编辑文件detect_large_files.sh后保存
bash 复制代码
#!/bin/bash

# 设置阈值,单位字节,100MB = 100 * 1024 * 1024
THRESHOLD=$((100 * 1024 * 1024))

# 遍历项目文件
find . -type f ! -path "./.git/*" | while read file; do
    size=$(stat -c%s "$file")
    if [ $size -gt $THRESHOLD ]; then
        ext="${file##*.}"
        echo "检测到大文件: $file (${size} bytes)"
        pattern="*.$ext"
        
        # 如果这个类型还没被 track,则添加 track 规则
        if ! grep -q "$pattern" .gitattributes 2>/dev/null; then
            echo "添加 Git LFS 跟踪规则: $pattern"
            git lfs track "$pattern"
        fi
    fi
done
 
  1. 赋予脚本执行权限:chmod +x detect_large_files.sh

5.运行脚本:./detect_large_files.sh

  1. 查看已经跟踪的文件:git lfs track

  2. 把改动提交:

git add .gitattributes

git commit

相关推荐
不爱吃糖的程序媛17 分钟前
鸿蒙三方库适配读懂 `thirdparty/AES/.gitignore`:哪些文件不该进 Git?
git·elasticsearch·harmonyos
天若有情67314 小时前
【C++原创开源】formort.h:一行头文件,实现比JS模板字符串更爽的链式拼接+响应式变量
开发语言·javascript·c++·git·github·开源项目·模版字符串
海盗123415 小时前
在群晖NAS上使用Git Server
git
y小花15 小时前
git常用指令
git
华科大胡子15 小时前
开源项目 Git 贡献全流程拆解
git
极地星光15 小时前
工程中:Git 子模块(submodule) vs 直接依赖(源码/库/包管理器)
git
无限进步_16 小时前
【C++&string】大数相乘算法详解:从字符串加法到乘法实现
java·开发语言·c++·git·算法·github·visual studio
无限进步_17 小时前
【C++】验证回文字符串:高效算法详解与优化
java·开发语言·c++·git·算法·github·visual studio
无限进步_21 小时前
【C++】重载、重写和重定义的区别详解
c语言·开发语言·c++·ide·windows·git·github
历程里程碑21 小时前
1 . Git本地操作:版本控制 跨平台协作 仓库核心
java·开发语言·数据结构·c++·git·gitee·github