【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

相关推荐
刺客xs9 小时前
git 入门常用命令
大数据·git·elasticsearch
LostSpeed10 小时前
git - github工程中不能包含大文件
git·github
pzx_00111 小时前
【GIT】删除远程文件
git
小白_ysf11 小时前
Git 命令操作完整指南(实际工作中常用命令)
git·代码上传
HealthScience11 小时前
怎么使用git下载huggingface的文件
git
huangjiazhi_12 小时前
git clone next InitializeSecurityContext failed错误
git
_oP_i13 小时前
git gui设置github sshkey
git·github
ergevv14 小时前
Git 子模块(Submodule)操作指南
git·子模块·submodule
阿拉伯柠檬15 小时前
Git原理与使用(一)
大数据·linux·git·elasticsearch·面试
chao_78916 小时前
双设备全栈开发最佳实践[mac系统]
git·python·macos·docker·vue·全栈