6

My pre-commit.com config looks like this:

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

If I try to commit I get this error:

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")

I would like to avoid the checking for unstaged files. I know what I do, and if I only want to commit some changes (not all), then I want to do this.

How to disable the check for unstaged files?

guettli
  • 25,042
  • 81
  • 346
  • 663

2 Answers2

6

From your output, you didn't stage any files for commit. so git itself prevented you from committing (not pre-commit) -- if you actually wanted to commit nothing you can use git commit --allow-empty. If you want to stage some files for commit, use git add, or use git commit -a

Note that there were no errors from pre-commit here -- only a warning telling you that unstaged things were temporarily removed so that partial commits can be linted successfully.


disclaimer: I'm the author of pre-commit

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • 2
    Thank you! I was blind. Maybe pre-commit could be more explicit here (for stupid and blind users like me). – guettli Jan 20 '21 at 18:42
  • I think pre-commit is much more than a simple helper. Why not use it for setting up python dev-environments which have requirements outside the pyhon world? – guettli Jan 20 '21 at 18:43
  • I'm not sure what you're asking specifically :S -- pre-commit works for all sorts of different programming languages (despite being written in python and being most popular in python) – anthony sottile Jan 20 '21 at 19:09
  • 1
    @AnthonySottile Is there a way to disable `pre-commit` auto staging any unstaged changes? I'd like my Git messages to correctly reflect what changes took place> Its quite annoying. – demberto Mar 05 '22 at 12:10
  • @demberto `pre-commit` will never touch the staging area -- so you probably have some rogue thing running `git add` – anthony sottile Mar 05 '22 at 15:22
  • @AnthonySottile idk then maybe its vs code itself but running `git` from command line with `--no-verify` doesn't cause that behaviour (i.e. pre-commit hooks don't get invoked) – demberto Mar 05 '22 at 16:37
-1

The pre-commit hook will use a way of iterating over the files in the changeset. There's probably something like git ls-files in there. If you use git ls-files -c then it will just show you the cached changes (the default) -- if you use git ls-files -o then it will show uncommitted (other) files as well.

If you're re-using someone else's hooks, then you'll need to investigate those hooks to determine if there's a way they can selectively disable the changes.

AlBlue
  • 23,254
  • 14
  • 71
  • 91
  • Your answer is about general pre-commit, but the question is about pre-commit.com hooks. – guettli Jan 20 '21 at 13:34
  • 1
    Sure, that's why I said you're going to have to look at the hooks that were provided by someone else. It's not a configuration option that exists or defaults and there aren't flags that are standardised. If you look at the code for the hooks, you now know what to look for. – AlBlue Jan 20 '21 at 13:40
  • You are right "look at the code" is almost always an answer (beside many other answers) – guettli Jan 20 '21 at 13:58