diff --git a/pre_commit_hooks/no_commit_to_branch.py b/pre_commit_hooks/no_commit_to_branch.py
index 22ee95e..0c75217 100644
--- a/pre_commit_hooks/no_commit_to_branch.py
+++ b/pre_commit_hooks/no_commit_to_branch.py
@@ -1,21 +1,25 @@
 from __future__ import print_function
 
 import argparse
-import sys
 
+from pre_commit_hooks.util import CalledProcessError
 from pre_commit_hooks.util import cmd_output
 
 
 def is_on_branch(protected):
-    branch = cmd_output('git', 'symbolic-ref', 'HEAD')
+    try:
+        branch = cmd_output('git', 'symbolic-ref', 'HEAD')
+    except CalledProcessError:
+        return False
     chunks = branch.strip().split('/')
     return '/'.join(chunks[2:]) == protected
 
 
-def main(argv=[]):
+def main(argv=None):
     parser = argparse.ArgumentParser()
     parser.add_argument(
-        '-b', '--branch', default='master', help='branch to disallow commits to',
+        '-b', '--branch', default='master',
+        help='branch to disallow commits to',
     )
     args = parser.parse_args(argv)
 
@@ -23,4 +27,4 @@ def main(argv=[]):
 
 
 if __name__ == '__main__':
-    sys.exit(main(sys.argv))
+    exit(main())

no-commit-to-branch on CI
As a safety net, we've started running pre-commit on CI, e.g.
```
git diff --name-only "$GIT_COMMIT"^..."$GIT_COMMIT" | xargs pre-commit run --files
```

one of our projects uses `no-commit-to-branch` and we just noticed this behavior on CI:
```
Don't commit to branch...................................................Failed
hookid: no-commit-to-branch

Traceback (most recent call last):
  File "/var/lib/jenkins/.cache/pre-commit/repoVUdrA6/py_env-python2.7/bin/no-commit-to-branch", line 11, in <module>
    load_entry_point('pre-commit-hooks==1.1.0', 'console_scripts', 'no-commit-to-branch')()
  File "/var/lib/jenkins/.cache/pre-commit/repoVUdrA6/py_env-python2.7/local/lib/python2.7/site-packages/pre_commit_hooks/no_commit_to_branch.py", line 22, in main
    return int(is_on_branch(args.branch))
  File "/var/lib/jenkins/.cache/pre-commit/repoVUdrA6/py_env-python2.7/local/lib/python2.7/site-packages/pre_commit_hooks/no_commit_to_branch.py", line 10, in is_on_branch
    branch = cmd_output('git', 'symbolic-ref', 'HEAD')
  File "/var/lib/jenkins/.cache/pre-commit/repoVUdrA6/py_env-python2.7/local/lib/python2.7/site-packages/pre_commit_hooks/util.py", line 28, in cmd_output
    raise CalledProcessError(cmd, retcode, proc.returncode, stdout, stderr)
pre_commit_hooks.util.CalledProcessError: (('git', 'symbolic-ref', 'HEAD'), 0, 128, u'', u'fatal: ref HEAD is not a symbolic ref\n')
```

most likely this is not really an issue with `pre-commit` but with the way our Jenkins jobs are set up (?). I worked around this by using `SKIP=no-commit-to-branch pre-commit run` but just thought I would let you know.
diff --git a/tests/check_no_commit_to_branch_test.py b/tests/check_no_commit_to_branch_test.py
index 99af938..7e39256 100644
--- a/tests/check_no_commit_to_branch_test.py
+++ b/tests/check_no_commit_to_branch_test.py
@@ -29,19 +29,22 @@ def test_master_branch(temp_git_dir):
         assert is_on_branch('master') is True
 
 
-def test_main_b_call(temp_git_dir):
-    with temp_git_dir.as_cwd():
-        cmd_output('git', 'checkout', '-b', 'other')
-        assert main(['-b', 'other']) == 1
-
-
 def test_main_branch_call(temp_git_dir):
     with temp_git_dir.as_cwd():
         cmd_output('git', 'checkout', '-b', 'other')
-        assert main(['--branch', 'other']) == 1
+        assert main(('--branch', 'other')) == 1
 
 
 def test_main_default_call(temp_git_dir):
     with temp_git_dir.as_cwd():
         cmd_output('git', 'checkout', '-b', 'anotherbranch')
-        assert main() == 0
+        assert main(()) == 0
+
+
+def test_not_on_a_branch(temp_git_dir):
+    with temp_git_dir.as_cwd():
+        cmd_output('git', 'commit', '--no-gpg-sign', '--allow-empty', '-m1')
+        head = cmd_output('git', 'rev-parse', 'HEAD').strip()
+        cmd_output('git', 'checkout', head)
+        # we're not on a branch!
+        assert main(()) == 0

pip install -e .
parse_log_pytest
requirements.txt
pytest coverage
apt-get update apt-get install -y gcc
3.6
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs==22.2.0
autopep8==2.0.4
certifi==2021.5.30
coverage==6.2
flake8==2.5.5
importlib-metadata==4.8.3
iniconfig==1.1.1
mccabe==0.4.0
packaging==21.3
pep8==1.7.1
pluggy==1.0.0
-e git+https://github.com/pre-commit/pre-commit-hooks.git@2f1e5e2abf7c3be32557ad1d70aea3be5c69c354#egg=pre_commit_hooks
py==1.11.0
pycodestyle==2.10.0
pyflakes==1.0.0
pyparsing==3.1.4
pytest==7.0.1
PyYAML==6.0.1
six==1.17.0
tomli==1.2.3
typing_extensions==4.1.1
zipp==3.6.0

name: pre-commit-hooks
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
  - certifi=2021.5.30=py36h06a4308_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - 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=1.1.1w=h7f8727e_0
  - pip=21.2.2=py36h06a4308_0
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - attrs==22.2.0
      - autopep8==2.0.4
      - coverage==6.2
      - flake8==2.5.5
      - importlib-metadata==4.8.3
      - iniconfig==1.1.1
      - mccabe==0.4.0
      - packaging==21.3
      - pep8==1.7.1
      - pluggy==1.0.0
      - py==1.11.0
      - pycodestyle==2.10.0
      - pyflakes==1.0.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pyyaml==6.0.1
      - six==1.17.0
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - zipp==3.6.0
prefix: /opt/conda/envs/pre-commit-hooks

tests/check_no_commit_to_branch_test.py::test_not_on_a_branch
tests/check_no_commit_to_branch_test.py::test_other_branch tests/check_no_commit_to_branch_test.py::test_multi_branch tests/check_no_commit_to_branch_test.py::test_multi_branch_fail tests/check_no_commit_to_branch_test.py::test_master_branch tests/check_no_commit_to_branch_test.py::test_main_branch_call tests/check_no_commit_to_branch_test.py::test_main_default_call
pre_commit_hooks/no_commit_to_branch.py
pre_commit_hooks/no_commit_to_branch.py