1

I am using this logic and need the only branch name that match this string "AMAZONFIX".

git for-each-ref --format='%(committerdate:short) %(authordate:relative) %(refname:lstrip=3)' --sort -committerdate refs/remotes/$REMOTE_NAME | while read date branch; do
printf "%s | %s %s %s %s %s | %50s\n" $date $branch

Output result :

feature/core-xyz
dummy/AMAZON-bitMap
bugfix/AMAZONFIX-work
main/outlet-file

Excepted Output :

dummy/AMAZONFIX-bitMap
bugfix/work-place-AMAZONFIX

Only the branch name that having "AMAZONFIX"

rowoc
  • 239
  • 2
  • 12

1 Answers1

0

If you do nnot want to use a post-processing step like | grep AMAZONFIX, consider the git for-each-ref --contains=... directive (introduced with Git 2.7, Q4 2015)

In your case, on multiple lines, for readability:

git for-each-ref \
  --format='%(committerdate:short) %(authordate:relative) 
            %(refname:lstrip=3)' 
  --contains=$REMOTE_NAME/AMAZONFIX --contains=AMAZONFIX 
  --sort -committerdate refs/remotes/origin | \
  while read date branch; \
    do printf "%s | %s %s %s %s %s | %50s\n" $date $branch; 
  done

However, that would only work for branches named AMAZONFIX, not xxxAMAZONFIXyyy

Since --contains does not support wildcard, grep remains the best option:

git for-each-ref...| while... ;done | grep AMAZONFIX
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • I tried same but getting Error as : error: malformed object name origin/AMAZONFIX @VonC – rowoc Nov 11 '21 at 13:54
  • @rowoc Indeed: it works on my side, but only because I have a branch named `AMAZONFIX`. In your case, a grep remains the best option. I have edited the answer accordingly. – VonC Nov 11 '21 at 13:59
  • Thanks it worked @VonC – rowoc Nov 11 '21 at 15:23