在“git checkout”失败后如何恢复“git lfs post-checkout”钩子

时间:2021-07-27 23:31:33

标签: git git-lfs

我在一个巨大的 git 单一存储库中工作,大小约为 100 GB。我们在 .git/hooks/post-checkout 中有一个 git post-checkout 钩子,它包含以下钩子,用于在每次结帐后运行 git lfs

#!/bin/sh
command -v git-lfs >/dev/null 2>&1 || { echo >&2 "\nThis repository is configured for Git LFS but 'git-lfs' was not found on your path. If you no longer wish to use Git LFS, remove this hook by deleting .git/hooks/post-checkout.\n"; exit 2; }
git lfs post-checkout "$@"

我刚刚运行了 git checkout main,在运行了 3 小时并通过互联网连接下载了 27 GB 的数据后,它失败了,因为我的磁盘已满,通过 git lfs 帖子完成了大约 97% -checkout 钩子操作。

所以,我清理了一些磁盘空间。

现在,git checkout main 失败并出现错误:

<块引用>

错误:您对以下文件的本地更改将被检出覆盖:

所以,我尝试手动运行 git lfs post-checkout main(我什至不知道这是否是一个合理的命令——我在这里猜测),但它也失败了:

<块引用>

这应该通过 Git 的 post-commit 钩子运行。运行 git lfs update 进行安装。

有什么方法可以恢复我的 git lfs 操作,这样我就不必清除刚刚下载的所有 27 GB 数据,然后从头开始重新下载(通过 git reset --hard && git clean -fd && git checkout main) ?

请注意,由于 git checkout main 结帐后挂钩操作,git lfs 显示了一些类似这样的错误:

error: Your local changes to the following files would be overwritten by checkout:
    [list of tons of files]
error: The following untracked working tree files would be overwritten by checkout:
    [list of tons of files]
Aborting
0

1 个答案:

答案 0 :(得分:0)

我的一个想法是注释掉 .git/hooks/post-checkout 目录中的最后一行,使其看起来像这样:

# git lfs post-checkout "$@"

然后,保存文件并运行 git checkout main。完成后,运行 git lfs pull,然后取消注释上面的行以使该文件恢复正常。这样,由于 git checkout alone 需要大约 10 分钟并检出大约 80000 个文件,因此该部分将在 git lfs 部分运行之前完成并通过需要永远(3 小时以上)并且可能会崩溃。

我不确定这是否有效,但无论如何,我没有一个确切的解决方案来解决我的问题,所以这就是我所做的:

  1. git checkout main 由于我的磁盘空间不足而失败。 git status 现在显示数千个已更改和未暂存的文件。因此,请运行以下命令来清除您的 git status,以准备再次运行 git checkout
    git status        # see that there are thousands of unstaged changes
    git reset --hard  # reset changed files back to how they are in `main`
    git clean -fd     # delete all unstaged files and directories
    git status        # this should now be clean
    
  2. 如果使用 Bazel 构建系统,则通过删除 bazel 缓存来减少磁盘使用量:
    df -h  # check current disk usage
    rm -rf ~/.cache/bazel  # delete Bazel build cache
    df -h  # ensure you have more space free now
    
  3. 减小 .git 文件夹的大小以释放磁盘空间。请在此处查看我的完整答案:How to shrink the .git folder
    time git lfs prune
    time git gc
    time git prune
    time git repack -a -d --depth=250 --window=250
    
  4. 现在再次尝试结帐,只需让它在 3 个多小时的过程中重新下载整个 30 GB 左右。 :( 哦,好吧……至少它现在会过去,因为我刚刚释放了磁盘空间 1) 减小了我的 .git 目录的大小和 2) 删除了我的 Bazel 构建缓存:
    time git checkout main    
    

完成。