我正在使用 git diff-index --check --cached HEAD --
修剪 git 提交中的空格。我想使用快照添加 Jest 测试,但快照文件包含空格,如果我删除它,我的测试总是会失败。所以我想从空白检查中排除 *.js.snap
文件。我如何告诉 git 从 git diff-index
中排除 *.js.snap
(或者,**/__snapshots/*
)文件>?我在 OSX 上使用 bash。
与此同时,我通过将我的提交 Hook 更改为交互式来解决这个问题:
# If there are whitespace errors, print the offending file names and fail.
git diff-index --check --cached HEAD --
if [ $? -ne 0 ]; then
# Allows us to read user input below, assigns stdin to keyboard
exec < /dev/tty
while true; do
echo "Your commit introduces trailing whitespace. Are you sure you want to commit? y/n"
read yn
case $yn in
y ) exit 0;;
n ) exit 1;;
esac
done
fi
最佳答案
Git 确实有一种指定要排除的路径的方法,尽管它的文档很少而且显然不是很为人所知。它被称为 pathspec , 在这种情况下可以按如下方式使用:
git diff-index --check --cached HEAD -- ':!*.js.snap' .
其中 :
是指定 pathspec 不只是常规路径的特殊字符,'!'指定匹配路径其余部分的文件应从匹配列表中排除。
不像 Mercurial 的 -X
或 --exclude
那样简单,但它就在那里。
关于git - 从 git 空白检查中排除 Jest 快照,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39537830/