git 挂钩以批量取消暂存路径中的所有文件

git hook to batch unstage all files in a path

我有一个 devlog 文件夹,我在其中写下了我的 thoughts/example 代码,我希望通过 git status 可以看到这些代码。排除这些文件后,我通常只能 git add .; git commit,但显然 devlog 中的内容禁止这样做。我喜欢有一个 git 挂钩,它可以在提交之前取消暂存整个路径及其内容。

我试过了:

git rm --cached devlog/*

但显然,它希望 ./devlog 的所有内容都已经上演(包括那些被 .gitignore 忽略的内容),因此抛出错误:

git rm --cached devlog/*
#> fatal: pathspec 'devlog/check.log' did not match any files

其中 devlog/check.log 被忽略,根本没有上演。

你可以:

  • 强制先添加check.log
  • 然后git rm --cached一切

由于 check.log 被忽略(在 .gitignore 中列出),它会在 git rm --cached.

之后自动(再次)被忽略

所以:

    git add --force devlog/check.log
    git rm --cached devlog/*

这是一个似乎有效的解决方案。只需向 .git/hooks/pre-commit 添加以下内容:

git reset -- devlog/*