迁移Git历史记录到Git LFS

Git LFS是Github开源的Git扩展,用于对大型文件进行版本控制,可以解决Github对单个文件限制100M的问题

迁移

先用gitlistobjectbysize.sh按文件大小排序列出大文件,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#!/bin/bash -e
# work over each commit and append all files in tree to $tempFile
tempFile=$(mktemp)
IFS=$'\n'
for commitSHA1 in $(git rev-list --all); do
git ls-tree -r --long "$commitSHA1" >>"$tempFile"
done

# sort files by SHA1, de-dupe list and finally re-sort by filesize
sort --key 3 "$tempFile" | \
uniq | \
sort --key 4 --numeric-sort --reverse | \
awk '!visited[$5]++' | \
awk '{ split( "B KB MB GB" , v ); s=1; while( $4>1024 ){ $4/=1024; s++ } print int($4) " " v[s] "\t\t" $5 }' | \
tac

# remove temp file
rm "$tempFile"

运行此脚本需要先安装tac

1
brew install coreutils

将列出的大文件或文件夹加入--include,多个以,分隔

1
2
3
4
5
6
7
8
// 加入assets文件夹 指定master分支 仅显示迁移信息不迁移
git lfs migrate info --include="src/main/assets" --include-ref=master

//执行迁移
git lfs migrate import --include="src/main/assets" --include-ref=master --verbose

//强制提交
git push --force