Git LFS와 pre-commit 훅으로 대용량 파일 자동 추적하기

Git LFS와 pre-commit 훅으로 대용량 파일 자동 추적하기

pre-commit 파일

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#!/usr/bin/env bash
set -euo pipefail

# ===== 설정: 임계값 100MB =====
THRESH=$((100*1024*1024))

# git-lfs 설치 여부 확인
if ! command -v git-lfs >/dev/null 2>&1; then
echo "[pre-commit] git-lfs not found. Run 'git lfs install' first." >&2
exit 1
fi

# 스테이징된 추가/수정 파일 목록
files=$(git diff --cached --name-only --diff-filter=AM -z | tr '\0' '\n')

for f in $files; do
[ -f "$f" ] || continue
size=$(stat -c%s "$f" 2>/dev/null || stat -f%z "$f")

if [ "$size" -ge "$THRESH" ]; then
echo "[LFS] '$f' is $size bytes (>= $THRESH). Tracking with Git LFS..."
git lfs track -- "$f" >/dev/null
git add .gitattributes "$f"
echo "[LFS] Tracked + re-staged: $f"
fi
done

exit 0

hooksPath 확인

1
git config --get core.hooksPath

로컬 설정

1
git config --local core.hooksPath .githooks

글로벌 설정

1
git config --global core.hooksPath home/beni/.githooks

참조