简体   繁体   中英

List all git local branches that start with string

I want to list all local branches (eventually delete but for safety's sake...) that ONLY begin with abc. The thing is that this kind of works. But if no branch starts with "abc" then it lists ALL of the branches. which is what I don't want to end up doing (deleting all my local branches)

git for-each-ref --format="%(refname:short)" refs/heads/abc\* | xargs git branch --list

You just need to tell xargs not to execute its command if it has no input.

You do that with the -r or --no-run-if-empty argument.

git for-each-ref --format="%(refname:short)" refs/heads/abc\* | xargs --no-run-if-empty git branch --list

To list all branches that begin with "abc":

git branch --list "abc*"

So if you want to delete them, then run the following:

git branch --list "abc*" | xargs --no-run-if-empty git branch --delete

You can append the --force flag to the command above if you want to live dangerously.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM