branches = [x.split()[-1].split('/')[-1] for x in ret.stdout.splitlines()]
     ret = []
     for branch in branches:
-        if fnmatch.fnmatch(branch.lower(), f'*{opts.branch.lower()}*'):
+        if branch.lower() == opts.branch.lower():
+            ret = [branch]
+            break
+        elif fnmatch.fnmatch(branch.lower(), f'*{opts.branch.lower()}*'):
             ret.append(branch)
     if not ret:
         print(f'error: could not match branch {opts.branch}', file=sys.stderr)
     return ret[0]
 
 
-def set_git_config(opts):
-    """Set .git/config settings in all the repos."""
+def get_repo_topdir(opts):
+    """Find the top dir of this repo client checkout."""
     topdir = os.getcwd()
     while True:
         rdir = os.path.join(topdir, '.repo')
             break
         topdir = os.path.dirname(topdir)
         assert topdir != '/'
+    return topdir
+
+
+def set_git_config(opts):
+    """Set .git/config settings in all the repos."""
+    topdir = get_repo_topdir(opts)
+    rdir = os.path.join(topdir, '.repo')
 
     def gcfg(path, *args):
         cmd = ['git', f'--git-dir={path}', 'config', 'user.email'] + list(args)
         cmd += ['-u', EXT_MANIFEST]
         cmd += ['--repo-url', REPO_URL]
 
-    if opts.ref:
+    if opts.ref and os.path.realpath(opts.ref) != os.path.realpath(get_repo_topdir(opts)):
         cmd += ['--reference', opts.ref]
 
     if opts.manifest: