diff --git a/pre_commit/staged_files_only.py b/pre_commit/staged_files_only.py
index a2978b9..c8ee9c2 100644
--- a/pre_commit/staged_files_only.py
+++ b/pre_commit/staged_files_only.py
@@ -45,7 +45,7 @@ def staged_files_only(cmd_runner):
         finally:
             # Try to apply the patch we saved
             try:
-                cmd_runner.run(['git', 'apply', patch_filename])
+                cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
             except CalledProcessError:
                 logger.warning(
                     'Stashed changes conflicted with hook auto-fixes... '
@@ -55,7 +55,7 @@ def staged_files_only(cmd_runner):
                 # by hooks.
                 # Roll back the changes made by hooks.
                 cmd_runner.run(['git', 'checkout', '--', '.'])
-                cmd_runner.run(['git', 'apply', patch_filename])
+                cmd_runner.run(('git', 'apply', patch_filename), encoding=None)
             logger.info('Restored changes from {0}.'.format(patch_filename))
     else:
         # There weren't any staged files so we don't need to do anything

Stashed changes lost if hook fails with non-UTF-8 diff containing trailing whitespace
Hi,

A colleague almost lost all the changes she was working on after launching a `git commit` (with zero file added) and `pre-commit` crashing without restoring its [patch](https://github.com/pre-commit/pre-commit/blob/master/pre_commit/staged_files_only.py#L15).

Here is the terminal message she got:

```
[WARNING] Stashed changes conflicted with hook auto-fixes... Rolling back fixes...
An unexpected error has occurred: CalledProcessError: Command: ['git', 'apply', 'C:\\Users\\toto\\.pre-commit\\patch1471341002']
```

This seems very similar to a past solved issue:
https://github.com/pre-commit/pre-commit/issues/176

I think it had to do with CRLF conversion.
I'm going to try to reproduce this.

diff --git a/tests/staged_files_only_test.py b/tests/staged_files_only_test.py
index 00f4cca..993d33d 100644
--- a/tests/staged_files_only_test.py
+++ b/tests/staged_files_only_test.py
@@ -280,3 +280,28 @@ def test_stage_non_utf8_changes(foo_staged, cmd_runner):
     with staged_files_only(cmd_runner):
         _test_foo_state(foo_staged)
     _test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
+
+
+def test_non_utf8_conflicting_diff(foo_staged, cmd_runner):
+    """Regression test for #397"""
+    # The trailing whitespace is important here, this triggers git to produce
+    # an error message which looks like:
+    #
+    # ...patch1471530032:14: trailing whitespace.
+    # [[unprintable character]][[space character]]
+    # error: patch failed: foo:1
+    # error: foo: patch does not apply
+    #
+    # Previously, the error message (though discarded immediately) was being
+    # decoded with the UTF-8 codec (causing a crash)
+    contents = 'ú \n'
+    with io.open('foo', 'w', encoding='latin-1') as foo_file:
+        foo_file.write(contents)
+
+    _test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')
+    with staged_files_only(cmd_runner):
+        _test_foo_state(foo_staged)
+        # Create a conflicting diff that will need to be rolled back
+        with io.open('foo', 'w') as foo_file:
+            foo_file.write('')
+    _test_foo_state(foo_staged, contents, 'AM', encoding='latin-1')

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest
apt-get update apt-get install -y gcc
3.9
requirements-dev.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
aspy.yaml==1.3.0
attrs==25.3.0
cached-property==2.0.1
coverage==7.8.0
distlib==0.3.9
exceptiongroup==1.2.2
filelock==3.18.0
flake8==7.2.0
iniconfig==2.1.0
jsonschema==4.23.0
jsonschema-specifications==2024.10.1
mccabe==0.7.0
mock==5.2.0
nodeenv==1.9.1
packaging==24.2
platformdirs==4.3.7
pluggy==1.5.0
-e git+https://github.com/pre-commit/pre-commit.git@f11338ccfa612e36a6c1f2dc688080ec08fd66b0#egg=pre_commit
pycodestyle==2.13.0
pyflakes==3.3.1
pyterminalsize==0.1.0
pytest==8.3.5
PyYAML==6.0.2
referencing==0.36.2
rpds-py==0.24.0
tomli==2.2.1
typing_extensions==4.13.0
virtualenv==20.29.3

name: pre-commit
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - ca-certificates=2025.2.25=h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.4.4=h6a678d5_1
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - ncurses=6.4=h6a678d5_0
  - openssl=3.0.16=h5eee18b_0
  - pip=25.0=py39h06a4308_0
  - python=3.9.21=he870216_1
  - readline=8.2=h5eee18b_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - tzdata=2025a=h04d1e81_0
  - wheel=0.45.1=py39h06a4308_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - aspy-yaml==1.3.0
      - attrs==25.3.0
      - cached-property==2.0.1
      - coverage==7.8.0
      - distlib==0.3.9
      - exceptiongroup==1.2.2
      - filelock==3.18.0
      - flake8==7.2.0
      - iniconfig==2.1.0
      - jsonschema==4.23.0
      - jsonschema-specifications==2024.10.1
      - mccabe==0.7.0
      - mock==5.2.0
      - nodeenv==1.9.1
      - packaging==24.2
      - platformdirs==4.3.7
      - pluggy==1.5.0
      - pycodestyle==2.13.0
      - pyflakes==3.3.1
      - pyterminalsize==0.1.0
      - pytest==8.3.5
      - pyyaml==6.0.2
      - referencing==0.36.2
      - rpds-py==0.24.0
      - setuptools==18.4
      - tomli==2.2.1
      - typing-extensions==4.13.0
      - virtualenv==20.29.3
prefix: /opt/conda/envs/pre-commit

tests/staged_files_only_test.py::test_non_utf8_conflicting_diff
tests/staged_files_only_test.py::test_foo_staged tests/staged_files_only_test.py::test_foo_nothing_unstaged tests/staged_files_only_test.py::test_foo_something_unstaged tests/staged_files_only_test.py::test_foo_something_unstaged_diff_color_always tests/staged_files_only_test.py::test_foo_both_modify_non_conflicting tests/staged_files_only_test.py::test_foo_both_modify_conflicting tests/staged_files_only_test.py::test_img_staged tests/staged_files_only_test.py::test_img_nothing_unstaged tests/staged_files_only_test.py::test_img_something_unstaged tests/staged_files_only_test.py::test_img_conflict tests/staged_files_only_test.py::test_diff_returns_1_no_diff_though tests/staged_files_only_test.py::test_stage_utf8_changes tests/staged_files_only_test.py::test_stage_non_utf8_changes
pre_commit/staged_files_only.py
pre_commit/staged_files_only.py