I have installed Git parameter plugin in jenkins. There is an option for filtering branches from Git which needs a regex. So what I get in my branches dropdown is "origin/master". I need a regular expression where I need only word "master" and want to exclude "origin/" from all branches name.
-
A bit unclear in what environment (language) you want to achieve this. Assuming it's in the shell, then `grep` and/or `sed` could be the tools for the job. – mkilmanas Jun 16 '17 at 05:59
-
i just need a regex – Harshad Solanki Jun 16 '17 at 06:02
3 Answers
Well, that's the thing with regexps - they don't quite "exclude" things, they only match (and that match can optionally be used for replace).
So you need to think more about the structure of the branch names and see what rule could describe it best.
#[^/]+$#
(#
is used as regexp delimiter here; if your system does not support arbitrary delimiters, the equivalent with slashes is /[^\/]+$/
which is harder to read but works just the same) could be one option - it will match everything until the end of the string/line that does not contain slashes. This works as long as your branch names do not contain more slashes (e.g. origin/feature/super-cool-stuff
would result in super-cool-stuff
where one might actually expect feature/super-cool-stuff
).
In other words there is no single correct answer to this without knowing what the branch naming rules are.

- 3,395
- 17
- 27
-
branches name are in format origin/branch_name .so i just need branch_name in my dropdown – Harshad Solanki Jun 16 '17 at 06:55
-
Well, great then. In that case the regexp provided here should work well for you. Please don't forget to upvote/accept the answer that solves your problem. – mkilmanas Jun 16 '17 at 07:06
-
-
That is very much vendor/implementation specific, most support arbitrary delimiters (I used `#`), but if yours doesn't, you can go with default (`/`) and escape the slash in the middle (see updated answer) – mkilmanas Jun 16 '17 at 08:43
-
Also, it could be that your system does not require/expect delimiters at all - in which case remove the first and last characters, i.e. use only `[^\/]+$` – mkilmanas Jun 16 '17 at 08:45
If you want to exclude origin/
from the branch name, then you can try only searching for local branches:
git branch | grep master
Note that git branch
with no options only searches local branches, -r
searches only remote tracking branches, and -a
searches everything.

- 502,043
- 27
- 286
- 360
You can use the strip
option of format:
git for-each-ref --format='%(refname:strip=3)' --shell --no-merged @ refs
# or
git branch -a --format='%(refname:strip=3)'
That way, refs/remotes/origin
is not part of the output, and you can grep on your remote branches.
Regarding Jenkins and its branch filtering in the "Branch to Build", you can use:
^master
In order to isolate master
only, not origin/master
.
You could use a negative lookahead as in this answer to exclude any branch with origin
:
:^(?!.*origin/).*$

- 1,262,500
- 529
- 4,410
- 5,250