mkdir ~/global_hooks
#create and edit the post-checkout inside
echo '#!/bin/bash' >> ~/global_hooks/post-checkout
echo 'rm -rf .git' >> ~/global_hooks/post-checkout
#...
chmod 755 ~/global_hooks/post-checkout
git config --global core.hooksPath ~/global_hooks/
任何带有结账功能的新克隆(没有-n
or --mirror
or --bare
)将触发~/global_hooks/post-checkout
and .git
将被删除。但这不是一个好主意,因为您必须为不同的克隆启用和禁用挂钩。而且每个用户都需要进行配置,有点烦人。
更糟糕的是,在没有禁用或覆盖全局钩子的情况下,存储库中的 git-checkout 将删除其.git
。所以这只是为了好玩,但根本不实用。
2022年6月2日编辑:
一些改进使其更加实用。
首先,不要跑步git config --global core.hooksPath ~/global_hooks/
.
二、更新~/global_hooks/post-checkout
#!/bin/bash
enable=$(git config --get my.fun)
if [[ "${enable}" = yes ]];then
echo Removing .git
rm -rf .git
fi
现在可以根据需要启用或禁用该挂钩。
默认情况下,新的克隆或签出不会调用挂钩,因此.git
是安全的。
当你真的想删除时.git
克隆后,添加2个选项git
.
git -c core.hookspath=~/global_hooks/ -c my.fun=yes clone $repo_url -b $branch
-c foo.bar=baz
是一个临时配置值,仅适用于此 git 命令。my.fun
是一个自定义的配置词,您可以将其替换为您喜欢的其他词。