user650309
user650309

Reputation: 2899

How do I search for branch names in Git?

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?

Upvotes: 158

Views: 121982

Answers (9)

Ramakrishna Talla
Ramakrishna Talla

Reputation: 1

Use below to search branch names like dev in local and remote

$ git branch --all | grep 'dev'
* dev
  dev_rama
  remotes/origin/Dotnet4_dev
  remotes/origin/dev

Use below to search branch names like dev in local only

$ git branch --list | grep 'dev'
* dev
  dev_rama

Use below to search branch names like dev in remote only

$ git branch --remote | grep 'dev'
  remotes/origin/Dotnet4_dev
  remotes/origin/dev

Upvotes: -1

user6609090
user6609090

Reputation:

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*"

Upvotes: 15

Shubhank Gupta
Shubhank Gupta

Reputation: 825

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

Upvotes: 0

Unmitigated
Unmitigated

Reputation: 89224

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*"

Upvotes: 1

Calvin Li
Calvin Li

Reputation: 2684

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

Upvotes: 122

Fosterdn007
Fosterdn007

Reputation: 49

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.

Upvotes: 0

Snekse
Snekse

Reputation: 15789

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

Upvotes: 11

Roy Shmuli
Roy Shmuli

Reputation: 5019

git branch -a | grep selector

Or

git branch -r | grep selector

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

Upvotes: 14

Evil Toad
Evil Toad

Reputation: 3242

git branch --all | grep <id>

Upvotes: 191

Related Questions