重置git lfs仓库指针

21

我已经检出了一个git LFS仓库。所有的二进制文件都是指针。我使用如下命令拉取了真正的二进制文件:

git lfs pull --include some/binaries

我曾使用二进制文件,现在我想要“取消提取(pull)”这些二进制文件并将它们重新转换为指针,以便我可以回收磁盘空间。

我没有找到任何适当的命令来完成这个操作,而且用 .git/lfs/objects 干预加上强制重置会让我感到紧张。

问题:如何将被跟踪的二进制文件转换回指针?

编辑:


你尝试过修改.gitattributes文件吗?请查看此链接:https://rehansaeed.com/gitattributes-best-practices/#git-large-file-system-lfs - Rodrigo
3个回答

12
一个未解决的LFS问题建议运行。
git read-tree HEAD && GIT_LFS_SKIP_SMUDGE=1 git checkout -f HEAD

来自问题的注意事项:

如果你在一个脏工作树中运行这个命令,你将会丢失你的更改,请不要这样做。

对我来说,在子模块中使用LFS时,运行这个命令是有效的。


11

正如您所提到的,Git LFS尚未实现该功能。 Git LFS用户正在使用脚本来实现相同的功能。 这是从您的链接中获取的有效脚本。

#!/bin/bash

lfs_files=($(git lfs ls-files -n))
for file in "${lfs_files[@]}"; do        
    git cat-file -e "HEAD:${file}" && git cat-file -p "HEAD:${file}" > "$file"
done
    
rm -rf .git/lfs/objects

它仅仅通过 git lfs ls-files 返回的所有文件创建一个列表,并通过迭代来替换文件为其指针。最后一行从本地仓库中删除所有git LFS对象。


完成此操作后,git status 会将所有这些文件显示为已修改。 - Praveen Lobo

0

它将文件转换回指针。 就像撤销git lfs pull一样。 :-)

#!/bin/bash

# ref: https://github.com/git-lfs/git-lfs/issues/1189#issuecomment-348013275
# ref: https://sabicalija.github.io/git-lfs-intro/

if [ $# -eq 0 ]; then
    echo "No input: quit."
    exit 1
fi

cur_size=$(du -sh . | awk -F " " '{print $1}')
for bfile in $@; do
    # to pointer
    # print file size
    f_size=$(du -sh $bfile | awk -F " " '{print $1}')
    mv $bfile $bfile.bak
    cat $bfile.bak | git lfs clean > $bfile
    rm $bfile.bak
    pt_size=$(du -h $bfile | awk -F " " '{print $1}')
    printf "%-30s: " $bfile
    printf "%s" $f_size
    printf " -> %s \n" $pt_size

    # delete cache in .git
    hash_value=$(cat $bfile | grep oid | cut -d ":" -f 2)
    cache_path=$(find . | grep $hash_value)
    if [ -z ${cache_path} ];then
        printf "#WARNING# oid can't find in cache: %s \n" $hash_value
    else
        rm $cache_path
        printf ">> Pruned cache at: %s \n" $cache_path
        git checkout -- $bfile
    fi
done

# report result
after_size=$(du -sh . | awk -F " " '{print $1}')
printf "Finish: Current directory size shrink"
printf "(%s" $cur_size
printf " -> %s).\n" $after_size
exit 0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接