我正在尝试编写一个git pre-commit钩子来检查在提交之前是否再次修改了使用git add
分段的文件。pre-commit钩子检查是否再次修改了分段文件
基本上我很感兴趣,这些文件
git status --short
MM test1
AM test2
现在pre-commit钩子看起来是这样的:
#!/bin/bash
# git pre-commit hook that checks for staged files that were modified.
# exit on error
set -e
# DOES NOT WORK
cached=$(git diff --name-only --diff-filter=M)
# everything is fine
if ! [[ -n $cached ]]; then
exit 0
fi
# GNU and BSD versions of xargs behave differently.
if xargs --version >/dev/null 2>&1; then
flag="-l" # GNU
else
flag="-L1" # BSD
fi
printf "The following files have been modified after they have been staged:\n\n"
printf "$cached"
printf "\n\nYou can stage these changes with:\n"
printf " git diff --name-only --cached | xargs ${flag} git add\n"
printf "Aborting commit. Stage the modified files and commit again or skip"
printf " checking with --no-verify (not recommended).\n"
exit 1
的问题是,这个钩子也拿起修改过的文件,但没有上演。我将如何调整我的钩子?