Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter remote branches in git

Tags:

git

git-branch

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?

like image 682
Ilias Karim Avatar asked Mar 07 '19 23:03

Ilias Karim


3 Answers

git branch -r --list origin/iliaskarim\*
like image 191
Ilias Karim Avatar answered Nov 10 '22 14:11

Ilias Karim


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
like image 30
Romain Valeri Avatar answered Nov 10 '22 14:11

Romain Valeri


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

like image 1
torek Avatar answered Nov 10 '22 16:11

torek