How do I search for branch names in Git?
Asked Answered
P

8

147

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?

Phia answered 3/8, 2015 at 12:51 Comment(2)
hello =] check the link(#15292891). it can help you.Venenose
@ThaisaMirely The link you provided is for searching within all files in all branches. This question is asking to just search the branch names.Wolfort
W
182
git branch --all | grep <id>
Warrior answered 3/8, 2015 at 12:57 Comment(3)
git branch --all | grep -i <id> for case insensitivity.Hymenopteran
@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.Warrior
See solution 2 for one that works in PowershellRaveaux
S
118

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.).

Susurration answered 28/8, 2017 at 21:41 Comment(5)
NOTE: This will only list local branches i.e. you won't be able to find the remote branches with this command.Highland
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.Susurration
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.Thunderstorm
@Facepalmed Added to the answerSusurration
can add --all flag to search also remote branches ´git branch --list --all *name*´ bonus it works in Windows PowershellRaveaux
A
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.

Aquarelle answered 3/8, 2015 at 12:57 Comment(0)
C
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*"
Cockney answered 15/1, 2020 at 11:47 Comment(4)
It is git branch --list <whatever>. What you wrote does not work and if you use it without the keyword, it creates a branch named listKendyl
seems it is just work on local branches, but how about remote branches?Slavonic
@Slavonic add flag --allRaveaux
Works fine on remote branches: git branch -r --list *production*Mitchelmitchell
M
11

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

Melendez answered 17/2, 2017 at 17:15 Comment(1)
Although both are using grep on branches list, this one is better than the accepted answer for frequent use. Thank you.Shroud
T
1

Multiple pattern can be passed to git branch using the -l or --list option. All branches that match the pattern(s) will be returned. The wildcard * can be used to match any branch that contains a substring in its name.

To filter on all branches, add the -a or --all option.

For example:

git branch -la "*containsThisSubstring*" "*withThisSuffix" "withThisPrefix*"
Thevenot answered 9/9, 2023 at 20:4 Comment(0)
L
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.

Leipzig answered 3/8, 2015 at 13:5 Comment(0)
S
0

You can use the git branch command along with grep to search for branches that start with a specific text. Here's the command:

git branch | grep "^<starting_text>"

Replace <starting_text> with the text you want to search for. This command will list all branches whose names start with the specified text.

For example, if you want to search for branches that start with "feature", you would use:

git branch | grep "^feature"

This will show you a list of branches starting with "feature".

Note: The command provided above only searches for local branches.

If you want to include remote branches as well, you can use the -a flag with the git branch command to list both local and remote branches, and then use grep to filter the results:

git branch -a | grep "^<starting_text>"

This command will search for both local and remote branches that start with the specified text. Replace <starting_text> with the text you want to search for.

e.g.

git branch -a | grep "^feature"

This command will list all branches, including local and remote, that start with "feature".

Shirashirah answered 12/2 at 10:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.