我正在嘗試編寫一個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
的問題是,這個鉤子也拿起修改過的文件,但沒有上演。我將如何調整我的鉤子?