【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

相关推荐
大卫小东(Sheldon)2 小时前
面向 Git 用户的 jujutsu 使用入门
git
自学也学好编程7 小时前
Git分支管理与工作流详解
git
自学也学好编程9 小时前
Git基础概念与常用命令详解
git
linrunxinnn12 小时前
Git 团队协作总结 —— 不只是版本控制的工具
git
吱吱02号机16 小时前
<Git>从零创建远程新仓库(最小操作)
git
测试开发技术1 天前
使用 Git 时出现 unable to access,如何解决?
git·面试题
zhougl9961 天前
git项目,有idea文件夹,怎么去掉
java·git·intellij-idea
tmacfrank2 天前
Git 使用技巧与原理(一)—— 基础操作
git
dilvx2 天前
git 配置 default editor
git