【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

相关推荐
AIMath~36 分钟前
Git 子模块(Submodule)目录结构清除实战复盘
git
切糕师学AI1 小时前
Ubuntu 下 Git 完全使用指南
linux·git·ubuntu
一袋米扛几楼983 小时前
【Git】规范化协作:详解 GitHub 工作流中的 Issue、Branch 与 Pull Request 最佳实践
前端·git·github·issue
尘埃落定wf4 小时前
# GitHub CLI:告别繁琐的 Git 命令,让开发更高效
git·github
恋喵大鲤鱼4 小时前
git clone
git·git clone
金牛IT6 小时前
Gogs 轻量级 Git 服务器搭建与使用
运维·服务器·git
Qres82116 小时前
Git安装记录
git
wj30558537818 小时前
Codex + Git 开发环境配置指南(WSL版)
linux·运维·git
楠枬20 小时前
Git 分支管理
git
奇怪的点1 天前
git clone失败
git