diff --git a/asv/benchmarks.py b/asv/benchmarks.py
index 40379f9..8dacfbb 100644
--- a/asv/benchmarks.py
+++ b/asv/benchmarks.py
@@ -19,6 +19,7 @@ import six
 from .console import log, truncate_left
 from . import util
 from . import statistics
+from .repo import NoSuchNameError
 
 
 WIN = (os.name == "nt")
@@ -410,7 +411,11 @@ class Benchmarks(dict):
         # discovery usually succeeds
         commit_hashes = list(commit_hashes)
         for branch in conf.branches:
-            branch_hash = repo.get_hash_from_name(branch)
+            try:
+                branch_hash = repo.get_hash_from_name(branch)
+            except NoSuchNameError:
+                continue
+
             if branch_hash not in commit_hashes:
                 commit_hashes.append(branch_hash)
 
diff --git a/asv/plugins/git.py b/asv/plugins/git.py
index bc9eb4d..04ee9d5 100644
--- a/asv/plugins/git.py
+++ b/asv/plugins/git.py
@@ -12,7 +12,7 @@ import os
 import re
 
 from ..console import log
-from ..repo import Repo
+from ..repo import Repo, NoSuchNameError
 from .. import util
 
 
@@ -127,8 +127,15 @@ class Git(Repo):
     def get_hash_from_name(self, name):
         if name is None:
             name = self.get_branch_name()
-        return self._run_git(['rev-parse', name],
-                             dots=False).strip().split()[0]
+
+        try:
+            return self._run_git(['rev-parse', name],
+                                 dots=False).strip().split()[0]
+        except util.ProcessError as err:
+            if err.stdout.strip() == name:
+                # Name does not exist
+                raise NoSuchNameError(name)
+            raise
 
     def get_hash_from_parent(self, name):
         return self.get_hash_from_name(name + '^')
diff --git a/asv/plugins/mercurial.py b/asv/plugins/mercurial.py
index e723e51..20cb626 100644
--- a/asv/plugins/mercurial.py
+++ b/asv/plugins/mercurial.py
@@ -17,7 +17,7 @@ except ImportError as exc:
     hglib = None
 
 from ..console import log
-from ..repo import Repo
+from ..repo import Repo, NoSuchNameError
 from .. import util
 
 
@@ -142,7 +142,12 @@ class Hg(Repo):
     def get_hash_from_name(self, name):
         if name is None:
             name = self.get_branch_name()
-        return self._decode(self._repo.log(self._encode(name))[0].node)
+        try:
+            return self._decode(self._repo.log(self._encode(name))[0].node)
+        except hglib.error.CommandError as err:
+            if b'unknown revision' in err.err:
+                raise NoSuchNameError(name)
+            raise
 
     def get_hash_from_parent(self, name):
         return self.get_hash_from_name('p1({0})'.format(name))
diff --git a/asv/repo.py b/asv/repo.py
index 8b7f98a..43e2f16 100644
--- a/asv/repo.py
+++ b/asv/repo.py
@@ -8,6 +8,13 @@ from __future__ import (absolute_import, division, print_function,
 from . import util
 
 
+class NoSuchNameError(RuntimeError):
+    """
+    Exception raised if requested branch or commit does not exist.
+    """
+    pass
+
+
 class Repo(object):
     """
     Base class for repository handlers.

`git rev-parse master` can fail
I'm trying to run benchmarks on Travis-CI. Unfortunately, "asv run" uses "git rev-parse master" to discover benchmarks, which fails as "master" doesn't necessary exist:
https://travis-ci.org/pitrou/arrow/builds/364763507#L866

The command being run is:
```
asv run --no-pull --show-stderr --quick ${TRAVIS_COMMIT}^!
```
diff --git a/test/test_benchmarks.py b/test/test_benchmarks.py
index 0a35fcb..4fa7a18 100644
--- a/test/test_benchmarks.py
+++ b/test/test_benchmarks.py
@@ -52,6 +52,7 @@ def test_find_benchmarks(tmpdir):
     d['env_dir'] = "env"
     d['benchmark_dir'] = 'benchmark'
     d['repo'] = tools.generate_test_repo(tmpdir, [0]).path
+    d['branches'] = ["master", "some-missing-branch"]  # missing branches ignored
     conf = config.Config.from_json(d)
 
     repo = get_repo(conf)
diff --git a/test/test_repo.py b/test/test_repo.py
index 88a498c..b10cb35 100644
--- a/test/test_repo.py
+++ b/test/test_repo.py
@@ -434,3 +434,35 @@ def test_root_ceiling(dvcs_type, tmpdir):
     # parent repository
     with pytest.raises(Exception):
         r.checkout(workcopy_dir, commit2)
+
+
+@pytest.mark.parametrize('dvcs_type', [
+    "git",
+    pytest.mark.skipif(hglib is None, reason="needs hglib")("hg")
+])
+def test_no_such_name_error(dvcs_type, tmpdir):
+    tmpdir = six.text_type(tmpdir)
+    dvcs = tools.generate_test_repo(tmpdir, values=[0], dvcs_type=dvcs_type)
+
+    conf = config.Config()
+    conf.branches = []
+    conf.dvcs = dvcs_type
+    conf.project = "project"
+    conf.repo = dvcs.path
+
+    r = repo.get_repo(conf)
+
+    # Check that NoSuchNameError error gets raised correctly
+    assert r.get_hash_from_name(None) == dvcs.get_hash(r._default_branch)
+    with pytest.raises(repo.NoSuchNameError):
+        r.get_hash_from_name("badbranch")
+
+    if dvcs_type == "git":
+        # Corrupted repository/etc should not give NoSuchNameError
+        util.long_path_rmtree(join(dvcs.path, ".git"))
+        with pytest.raises(Exception) as excinfo:
+            r.get_hash_from_name(None)
+        assert excinfo.type not in (AssertionError, repo.NoSuchNameError)
+    elif dvcs_type == "hg":
+        # hglib seems to do some caching, so this doesn't work
+        pass

pip install -e .[dev]
parse_log_pytest
requirements.txt
pytest pytest-cov pytest-xdist pytest-rerunfailures
apt-get update apt-get install -y gcc
3.6
pip_requirements.txt
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
alabaster==0.7.13
-e git+https://github.com/airspeed-velocity/asv.git@1ca6450ea2124967ce77379a0e5fb7dfb66a05cb#egg=asv
attrs==22.2.0
Babel==2.11.0
certifi==2021.5.30
charset-normalizer==2.0.12
coverage==6.2
docutils==0.18.1
execnet==1.9.0
idna==3.10
imagesize==1.4.1
importlib-metadata==4.8.3
iniconfig==1.1.1
Jinja2==3.0.3
MarkupSafe==2.0.1
packaging==21.3
pluggy==1.0.0
py==1.11.0
Pygments==2.14.0
pyparsing==3.1.4
pytest==7.0.1
pytest-cov==4.0.0
pytest-rerunfailures==10.3
pytest-xdist==3.0.2
pytz==2025.2
requests==2.27.1
six==1.17.0
snowballstemmer==2.2.0
Sphinx==5.3.0
sphinx-bootstrap-theme==0.8.1
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
tomli==1.2.3
typing_extensions==4.1.1
urllib3==1.26.20
zipp==3.6.0

name: asv
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:
      - alabaster==0.7.13
      - attrs==22.2.0
      - babel==2.11.0
      - charset-normalizer==2.0.12
      - coverage==6.2
      - docutils==0.18.1
      - execnet==1.9.0
      - idna==3.10
      - imagesize==1.4.1
      - importlib-metadata==4.8.3
      - iniconfig==1.1.1
      - jinja2==3.0.3
      - markupsafe==2.0.1
      - packaging==21.3
      - pluggy==1.0.0
      - py==1.11.0
      - pygments==2.14.0
      - pyparsing==3.1.4
      - pytest==7.0.1
      - pytest-cov==4.0.0
      - pytest-rerunfailures==10.3
      - pytest-xdist==3.0.2
      - pytz==2025.2
      - requests==2.27.1
      - six==1.17.0
      - snowballstemmer==2.2.0
      - sphinx==5.3.0
      - sphinx-bootstrap-theme==0.8.1
      - sphinxcontrib-applehelp==1.0.2
      - sphinxcontrib-devhelp==1.0.2
      - sphinxcontrib-htmlhelp==2.0.0
      - sphinxcontrib-jsmath==1.0.1
      - sphinxcontrib-qthelp==1.0.3
      - sphinxcontrib-serializinghtml==1.1.5
      - tomli==1.2.3
      - typing-extensions==4.1.1
      - urllib3==1.26.20
      - zipp==3.6.0
prefix: /opt/conda/envs/asv

test/test_repo.py::test_no_such_name_error[git]
test/test_benchmarks.py::test_find_benchmarks test/test_benchmarks.py::test_find_benchmarks_cwd_imports test/test_benchmarks.py::test_quick test/test_benchmarks.py::test_code_extraction test/test_repo.py::test_git_submodule test/test_repo.py::test_root_ceiling[dvcs_type1] test/test_repo.py::test_no_such_name_error[dvcs_type1]
test/test_benchmarks.py::test_invalid_benchmark_tree test/test_benchmarks.py::test_table_formatting test/test_repo.py::test_repo_git test/test_repo.py::test_repo_git_annotated_tag_date test/test_repo.py::test_get_branch_commits[git] test/test_repo.py::test_get_new_branch_commits[git-all] test/test_repo.py::test_get_new_branch_commits[git-new] test/test_repo.py::test_get_new_branch_commits[git-no-new] test/test_repo.py::test_get_new_branch_commits[git-new-branch-added-in-config] test/test_repo.py::test_root_ceiling[git]
asv/plugins/mercurial.py asv/benchmarks.py asv/plugins/git.py asv/repo.py
asv/plugins/mercurial.py asv/benchmarks.py asv/plugins/git.py asv/repo.py