Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Trim the space after Select-String commande PowerShell ( for removing multiple local git branch)

My objective is removing all branch starting with ‘fea’.

PS> git branch

  bug/5646
  chin-dev
* dev
  fea/account-manager
  fea/animation
  fea/charts
  fea/chat
  fea/dataTable
  fea/enquete
  fea/page-title
  fea/questionnaire
  fea/style
  hotfix
  master
  merge
  mod/dataTable
  question
  saif-dev
  support

The commande i tried

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

PS> git branch -d $(git branch | Select-String -Pattern 'fea/')

(Yes, I kown Select-String is matching string, but anyway ignore in that case)

Result with the commande

error: branch '  fea/account-manager' not found.
...
error: branch '  fea/questionnaire' not found.
error: branch '  fea/style' not found.

As you can see it return the matched line also with space.
So these anyway removing space with select-string?

Also i tried convert the result toString for using Trim()

PS > $(git branch | Select-String -Pattern 'fea/' ).ToString().Trim()
System.Object[]

>Solution :

  1. you can list a set of branches using git branch alone (and no external commands) :
git branch --list "fea/*"
  1. git branch now supports the --format=... option, which makes it much more suitable for scripting purposes (e.g: no need to do manual processing of the output to remove a leading * or leading spaces):
git branch --format="%(refname:short)" --list "fea/*"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading