Thursday, May 2, 2024
19
rated 0 times [  19] [ 0]  / answers: 1 / hits: 1618  / 2 Years ago, fri, january 7, 2022, 10:51:59

I wrote a command to delete all git branches without needing to write them all mannually, which is currently like this:


git branch -d $(git branch)

It does what I want, but also tries to delete branches named as files in current directory, so the output is something like this:


error: branch 'angular.json' not found.
error: branch 'assembly.xml' not found.
error: branch 'ci-settings.xml' not found.
error: branch 'coverage' not found.
error: branch 'cypress.json' not found.
error: branch 'Dockerfile.angular' not found.
error: branch 'Dockerfile.spring' not found.
error: branch 'e2e' not found.
error: branch 'examples' not found.
error: branch 'mvnw' not found.
error: branch 'mvnw.cmd' not found.
error: branch 'nginx' not found.
error: branch 'node' not found.
error: branch 'node_modules' not found.
error: branch 'package.json' not found.
error: branch 'package-lock.json' not found.
error: branch 'pom.xml' not found.
error: branch 'README.md' not found.
error: branch 'scripts' not found.
error: branch 'serve-proxy.conf.json' not found.
error: branch 'src' not found.
error: branch 'target' not found.
error: branch 'to-do.txt' not found.
error: branch 'tsconfig.json' not found.
error: branch 'web.config' not found.
error: Cannot delete branch 'dev' checked out at 'D:/Documentos/oreons/Rise/archivekeeper-ui'
Deleted branch test (was 729628a).

If I echo $(git branch), the output is like this:


$ echo $(git branch)
angular.json assembly.xml ci-settings.xml coverage cypress.json Dockerfile.angular Dockerfile.spring e2e examples mvnw mvnw.cmd nginx node node_modules package.json package-lock.json pom.xml README.md scripts serve-proxy.conf.json src target to-do.txt tsconfig.json web.config dev

Which explains the command's output. But why does $(git branch) outputs all these file names besides the git branches?


More From » command-line

 Answers
2

As @steeldriver mentions in the above comment, git branch has an asterisk (*) in its output to mark the currently checked-out branch.


$ git branch
* main
test

When running it as $(git branch), that asterisk in the output is expanded to all non-hidden files and directories in the directory you're currently in.


To remove the asterisk, you can format the output by passing the --format option by only showing the short format of the refnames.


$ git branch --format='%(refname:short)'
main
test

So the command you want to use would be the following.


git branch -d $(git branch --format='%(refname:short)')

[#76] Sunday, January 9, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
darpose

Total Points: 424
Total Questions: 99
Total Answers: 121

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
darpose questions
Sun, Jan 23, 22, 04:32, 2 Years ago
Tue, Apr 25, 23, 23:44, 1 Year ago
Wed, Dec 15, 21, 14:42, 2 Years ago
Wed, Jun 2, 21, 23:41, 3 Years ago
;