How to Search for Local and Remote Git Branches in Your Terminal

Sometimes I find myself needing to switch back to an old branch that I can not remember the name of. But I do remember a specific word the branch name contains. So how do I search that branch name. Here is how to do it with git branch
piping to grep
Make sure you are on the project directory. And run the below command.
$ git branch -a | grep <word>
For example. If I have tons of branches and I want to list all the branches that includes that word test
, we would run the command like this
$ git branch -a | grep test
If you want to do a case-insensetive search you can also pass the -i
flag.
$ git branch -a | grep -i TeSt
And it will list out all the avaialble local and remote branches with the word test
in it.
the
-a
flag ensures that all local and remote branches are listed. If you want to list only the local ones just run the the command without the-a
flag. And if you want list the remote ones only pass the-r
flag instead of the-a
flag.
Fore more information check out the man page of git branch
man git branch