【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

相关推荐
T - mars9 小时前
Pycharm中使用git
ide·git·pycharm
jason_yang11 小时前
Workspace搭建Vue3+组件分离的Monorepo项目
git·npm·前端工程化
鸽鸽程序猿15 小时前
【Git】Git 简介及基本操作
git
ziyue75751 天前
idea终端添加git-bash,支持linux的shell语法
linux·git·bash·idea·软件
风也温柔☆1 天前
idea 拉取分支git pull报错 The branch to pull from should be selected
git·intellij-idea·debug·git pull
博闻善行1 天前
git基础操作
运维·git
黑面前端小书生1 天前
Git 下载 、安装、规范
git
Rhys..2 天前
Git & GitHub 个人账户创建教程
git·github
wuyunhang1234562 天前
Git----常用指令入门
git
越千年2 天前
Git的“后悔药”全指南:reset、revert、restore 如何优雅地拯救手残
git