Git Clone 之后自动运行任务,无需使用 git hooks

Cod*_*rer 2 git bash terminal

git clone ..repo..在终端内运行后是否可以自动运行 .bash 脚本?起初,我想在结帐后挂钩内运行类似 和 的命令,但该文件只能在本地使用rm -rf .gitcomposer install那么还有其他选择可以做到这一点吗?

Elp*_*Kay 5

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/
Run Code Online (Sandbox Code Playgroud)

任何具有签出功能的新克隆(不带-n--mirror--bare)都将触发~/global_hooks/post-checkout并被.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
Run Code Online (Sandbox Code Playgroud)

现在可以根据需要启用或禁用该钩子。

默认情况下,新的克隆或签出不会调用挂钩,因此是.git安全的。

如果您确实想.git在克隆后将其删除,请向git.

git -c core.hookspath=~/global_hooks/ -c my.fun=yes clone $repo_url -b $branch
Run Code Online (Sandbox Code Playgroud)

-c foo.bar=baz是一个临时配置值,仅适用于此 git 命令。my.fun是一个自定义的配置词,您可以将其替换为您喜欢的其他词。