我在这里添加了所有答案,以添加一些让我的生活更舒适的代码。期望 BSD coreutils(如在 MacOS 上)。
git-overlook
#!/usr/bin/env bash
path="$1"
root="$(git rev-parse --show-toplevel)"
if [ -z "$path" ]; then
echo "Specify a path" 1>&2
exit 1;
fi;
if [ ! -d .git/overlook ]; then
mkdir $root/.git/overlook
fi;
record="$root/.git/overlook/$(echo $path | sed 's/\./___/g' | sed 's/\//____/g')"
touch "$record"
git update-index --skip-worktree "$path"
git-relook
#!/usr/bin/env bash
path="$1"
root="$(git rev-parse --show-toplevel)"
record="$root/.git/overlook/$(echo $path | sed 's/\./___/g' | sed 's/\//____/g')"
if [ -f "$record" ]; then
git update-index --no-skip-worktree "$path"
rm "$record"
fi;
最后一个.git/hooks/pre-commit
#!/bin/bash
set -eu
root="$(git rev-parse --show-toplevel)"
get_record_name() {
record="$root/.git/overlook/$(echo $1 | sed 's/\./___/g' | sed 's/\//____/g')"
echo "$record"
}
for file in $(git ls-files -v | grep ^S | sed 's/S //'); do
record="$(get_record_name $file)"
record_mtime="$(stat -f %m $record)"
file_mtime="$(stat -f %m $file)"
if [ "$record_mtime" -lt "$file_mtime" ]; then
echo "File $file is overlooked but has been modified since. Please run either git overlook $file or git relook $file" 1>&2
exit 1
else
echo "File $file is overlooked but hasn't been modified"
fi;
done;
这使得我可以运行git overlook path/to/file
,然后我可以继续以我快乐的方式提交,直到我更改了被忽略的文件,此时我必须再次忽略它或将它带回来git relook path/to/file
。显然,如果忽略的文件是我所做的唯一更改,然后我在git status
.
它会制作一些文件.git/overlook
来跟踪内容,但据我所知,git
它将与它们和平共存。