【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

相关推荐
凯子坚持 c11 小时前
Git 多人协作深度解析:从工作流模拟到仓库维护
git
要站在顶端13 小时前
克隆大型仓库卡住(7%每次就卡住了)
git
五月底_14 小时前
上传大量文件到github repo
git·github
rannn_1111 天前
【Git教程】概述、常用命令、Git-IDEA集成
java·git·后端·intellij-idea
春日见1 天前
虚拟机上由于网络问题无法正常git clone
linux·服务器·网络·人工智能·git·ubuntu·debug
冉佳驹1 天前
Linux ——— Git的核心操作流程、进程状态及环境变量相关知识
linux·git·进程·环境变量·进程状态·fork
cherry有点甜·1 天前
【git】git为什么会出现双向合并
git
笨鸟不是菜鸟1 天前
gitignore文件如何添加忽略文件或文件夹
git
灰色人生qwer1 天前
git add . 添加超长文件名报错了怎么办?
git·python·elasticsearch
星海拾遗1 天前
git rebase记录
大数据·git·elasticsearch