bit*_*inn 5 git bash github unity-game-engine git-lfs
我们要防止:
git而不是git-lfs,因为它们会增加 git 历史记录。鉴于:
问题:
git add一个大文件并且它没有被处理时看到警告消息git-lfs?(我们的环境是macOS。我看了很多解决方案,目前没有一个能满足我们的需求)
好吧,在 CodeWizard 和这个 SO 答案的帮助下,我自己成功创建了一个很好的指南:
首先,设置您的存储库core.hooksPath:
git config core.hooksPath .githooks
Run Code Online (Sandbox Code Playgroud)
其次,在文件夹内创建此pre-commit文件.githooks,以便可以跟踪它(要点链接),然后记住使用 授予它执行权限chmod +x。
#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments. The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".
# Redirect output to stderr.
exec 1>&2
FILE_SIZE_LIMIT_KB=1024
CURRENT_DIR="$(pwd)"
COLOR='\033[01;33m'
NOCOLOR='\033[0m'
HAS_ERROR=""
COUNTER=0
# generate file extension filter from gitattributes for git-lfs tracked files
filter=$(cat .gitattributes | grep filter=lfs | awk '{printf "-e .%s$ ", $1}')
# before git commit, check non git-lfs tracked files to limit size
files=$(git diff --cached --name-only | sort | uniq | grep -v $filter)
while read -r file; do
if [ "$file" = "" ]; then
continue
fi
file_path=$CURRENT_DIR/$file
file_size=$(ls -l "$file_path" | awk '{print $5}')
file_size_kb=$((file_size / 1024))
if [ "$file_size_kb" -ge "$FILE_SIZE_LIMIT_KB" ]; then
echo "${COLOR}${file}${NOCOLOR} has size ${file_size_kb}KB, over commit limit ${FILE_SIZE_LIMIT_KB}KB."
HAS_ERROR="YES"
((COUNTER++))
fi
done <<< "$files"
# exit with error if any non-lfs tracked files are over file size limit
if [ "$HAS_ERROR" != "" ]; then
echo "$COUNTER files are larger than permitted, please fix them before commit" >&2
exit 1
fi
exit 0
Run Code Online (Sandbox Code Playgroud)
现在,假设您已正确设置两者.gitattributes,则当您尝试并确保 git-lfs 未跟踪的所有暂存文件(如您的 中指定)将满足指定的文件大小限制时,git-lfs此预提交挂钩将运行。git commit.gitattributes
您的存储库的任何新用户都需要core.hooksPath自行设置,但除此之外,一切都应该正常工作。
希望这可以帮助其他 Unity 开发人员应对不断增长的 git 存储库大小!
- 我们如何可靠地防止大文件被添加到提交?
- 这可以通过存储库中的配置文件来完成,以便所有用户都优雅地遵循此规则吗?由于 GitHub 不支持服务器端挂钩,您可以使用客户端挂钩。正如您可能知道的那样,这些钩子可以毫无问题地传递和禁用,但这仍然是一个很好的方法。
core.hooksPathGit v2.9添加了在远程文件夹上设置客户端挂钩的功能。在此之前,挂钩必须已放置在.git文件夹内。
这将允许您编写脚本并将它们放在任何地方。我假设您知道什么是钩子,但如果不知道,请随时询问。
通常,您将挂钩放置在存储库(或任何其他常见文件夹)中。
# set the hooks path. for git config, the default location is --local
# so this configuration is locally per project
git config core.hooksPath .githooks
Run Code Online (Sandbox Code Playgroud)