预提交:如何禁用对未暂存文件的检查?

pre-commit: How to disable the check for unstaged files?

我的 pre-commit.com 配置如下所示:

repos:
-   repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0
    hooks:
    -   id: trailing-whitespace
    -   id: end-of-file-fixer
    -   id: check-yaml
    -   id: check-added-large-files
-   repo: https://gitlab.com/pycqa/flake8
    rev: ''  # pick a git hash / tag to point to
    hooks:
    -   id: flake8
-   repo: https://github.com/pre-commit/mirrors-isort
    rev: v5.7.0
    hooks:
    - id: isort

如果我尝试提交,我会收到此错误:

guettli@yoga15:~/projects/foo$ LANG=C git commit

[WARNING] Unstaged files detected.
[INFO] Stashing unstaged files to /home/guettli/.cache/pre-commit/patch1611141286.
Trim Trailing Whitespace.............................(no files to check)Skipped
Fix End of Files.....................................(no files to check)Skipped
Check Yaml...........................................(no files to check)Skipped
Check for added large files..........................(no files to check)Skipped
flake8...............................................(no files to check)Skipped
isort................................................(no files to check)Skipped
[INFO] Restored changes from /home/guettli/.cache/pre-commit/patch1611141286.

On branch feature/super-foo
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   .gitignore

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    .pre-commit-config.yaml

no changes added to commit (use "git add" and/or "git commit -a")

我想避免检查未暂存的文件。我知道我在做什么,如果我只想提交一些更改(不是全部),那么我想这样做。

如何禁用未暂存文件的检查?

预提交挂钩将使用一种遍历变更集中文件的方法。那里可能有类似 git ls-files 的东西。如果您使用 git ls-files -c 那么它只会显示缓存的更改(默认)——如果您使用 git ls-files -o 那么它也会显示未提交的(其他)文件。

如果您正在重复使用其他人的挂钩,那么您将需要调查这些挂钩以确定他们是否可以有选择地禁用更改。

根据您的输出,您没有暂存任何要提交的文件。所以 git 本身会阻止你提交(而不是 pre-commit)——如果你真的什么都不想提交,你可以使用 git commit --allow-empty。如果要暂存一些文件以供提交,请使用 git add,或使用 git commit -a

请注意,此处 pre-commit 没有任何错误 -- 只是一个警告,告诉您未暂存的内容已被暂时删除 so that partial commits can be linted successfully


免责声明:我是预提交的作者