137

I'd like to find a specific branch, and I know that its name would contain a specific substring (the id of the issue from our bug tracker), but I don't know the whole name of the branch (this is what I want to find out).

How can I search for this branch?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user650309
  • 2,639
  • 7
  • 28
  • 47
  • hello =] check the link(http://stackoverflow.com/questions/15292391/is-it-possible-to-perform-a-grep-search-in-all-the-branches-of-git-project). it can help you. – Thaisa Mirely Aug 03 '15 at 13:10
  • 2
    @ThaisaMirely The link you provided is for searching within **all files in all branches**. This question is asking to just search the **branch names**. – Steve Chambers Jul 28 '16 at 08:17

6 Answers6

177
git branch --all | grep <id>
Evil Toad
  • 3,053
  • 2
  • 19
  • 28
  • 27
    `git branch --all | grep -i ` for case insensitivity. – Adam Johns Feb 01 '18 at 16:58
  • @Evil Toad Your answer seems to be incomplete, because both the above commands give out 3 entries 1 matching branch name, and 2 other matching remote counterparts, how to get the first entry without the asterisk(*) ? – Vicky Dev Aug 19 '21 at 12:13
  • @VickyDev it seems like what you are asking for is not exactly what the OP was asking for. If you require a command to get a different result maybe you should post a new question. – Evil Toad Aug 19 '21 at 12:17
  • 1
    See solution 2 for one that works in Powershell – OrigamiEye Mar 24 '22 at 08:05
116

Git 1.7.8 offers a solution without using grep:

git branch --list <pattern> and in bash git branch --list '<pattern>' with quotes around pattern.

This works with wildcards (*) as well, so you can do use git branch --list *<id>* to find your branch.

This filters the list of branch names returned by the rest of your git branch command (for example, local branches only by default, all branches with git branch -a --list <pattern>, etc.).

Calvin Li
  • 2,614
  • 3
  • 17
  • 25
  • 7
    NOTE: This will only list local branches i.e. you won't be able to find the remote branches with this command. – Kinaan Khan Sherwani Jan 01 '18 at 09:14
  • 14
    This is because `git branch` by default, only lists local branches. Use `git branch -r` for remote branches and `git branch -a` for all branches. – Calvin Li Jan 02 '18 at 18:01
  • 4
    In bash, you're going to need to quote the glob expression to prevent it expanding. To test, try `git checkout -b JIRA-style-test-1 && git branch --list *test*` vs `git branch --list "*test*"`. Only the second will match `JIRA-style-test-1`. – Steven Kalt Jul 11 '18 at 17:49
  • 1
    @Facepalmed Added to the answer – Calvin Li Sep 25 '18 at 19:00
  • 1
    can add --all flag to search also remote branches ´git branch --list --all *name*´ bonus it works in Windows Powershell – OrigamiEye Mar 24 '22 at 08:04
14
git branch -a | grep selector

Or

git branch -r | grep selector

-a shows all local and remote branches, while -r shows only remote branches.

Roy Shmuli
  • 4,979
  • 1
  • 24
  • 38
13

Find the branch Name where having specified text

git branch --list *text*

For Ex- I want to find the branch that has "production" word

git branch --list *production*
10

Building of the answers that others have given, I added this to my .gitconfig

[alias]
  findb = "!f(){ git branch -ra | grep $1; }; f"

So on the command line I can type git findb BUG-123

Snekse
  • 15,474
  • 10
  • 62
  • 77
  • 1
    Although both are using grep on branches list, this one is better than the accepted answer for frequent use. Thank you. – Saim Oct 05 '17 at 21:47
0

Assuming that you are using GitHub, there is a drop down list for your branches and for tags in that drop down menu there is a search area.

You can use that search area to search for your branch name.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131