'Filter remote branches in git

I’d like git branch -a to only show a subset of remote branches, e.g. branches that begin with prefix like 'origin/iliaskarim'

What is the nicest way to accomplish this?



Solution 1:[1]

Assuming you're in a bash context, just pipe (|) to a grep ?

git branch -a | grep origin/iliaskarim

Then if you want both this subset of remote branches and all your local branches, maybe consider just chaining commands?

git branch -a | grep origin/iliaskarim; git branch

Solution 2:[2]

The front end git branch command does not have an easy way to do this.

If you use a namespace-like prefix, though—such as origin/iliaskarim/ (note the trailing slash)—git for-each-ref, which is the plumbing command that implements git branch, does have a nice way to do this. In particular:

git for-each-ref refs/remotes/origin/iliaskarim

suffices to iterate over those names, and only those names. You will still have to put this together with additional commands and/or options to get the effect of a limited git branch -a.

(Edit: as phd notes in a comment, you can also use a pattern match with an explicit glob-star: refs/remotes/origin/iliaskarim*, if you don't use a slash separator here. Remember to protect the star from the shell, if using a shell.)

Using grep -v as a pipe to filter away names that include remotes/origin/ but do not continue on with iliaskarim is another option. (See RomainValeri's answer; the idea here is to invert the test, dropping branches that do match some regular expression. Coming up with a suitable R.E., which depends on which expression syntax or syntaxes your grep supports, is left as an exercise. :-) )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2