-2

I have a bash alias to delete all the git branches which were also deleted on the remote Delete local Git branches after deleting them on the remote repo.

This is the command:

alias gbpurge="git checkout main; git pull; git fetch --all -p; git branch -vv | grep gone | awk '{ print $1 }' | xargs -n 1 git branch -d"

The output of git branch -vv | grep gone is:

  small-fixes                        dc454c7 [origin/small-fixes: gone] Removed superfluous tailwind class
  style-elements                     2103b56 [origin/style-elements: gone] Added design elements (background images etc.)

When I copy paste the content between the ' marks to the console this command works as expected and prints:

error: The branch 'small-fixes' is not fully merged.
If you are sure you want to delete it, run 'git branch -D small-fixes'.
error: The branch 'style-elements' is not fully merged.
If you are sure you want to delete it, run 'git branch -D style-elements'.

(These errors are expected here and not a problem)

however when I use the alias it prints:

error: The branch 'small-fixes' is not fully merged.
If you are sure you want to delete it, run 'git branch -D small-fixes'.
error: branch 'dc454c7' not found.
error: branch '[origin/small-fixes:' not found.
error: branch 'gone]' not found.
error: branch 'Removed' not found.
error: branch 'superflous' not found.
error: branch 'tailwind' not found.
error: branch 'class' not found.
error: The branch 'style-elements' is not fully merged.
If you are sure you want to delete it, run 'git branch -D style-elements'.
error: branch '2103b56' not found.
error: branch '[origin/style-elements:' not found.
error: branch 'gone]' not found.
error: branch 'Added' not found.
error: branch 'design' not found.
error: branch 'elements' not found.
error: branch '(background' not found.
error: branch 'images' not found.
error: branch 'etc.)' not found.

Why is it forwarding the commit message words as branch names in the alias?


Edit - changed outside quotes to double quotes and inside quotes aroudn $1 to single quotes

sev
  • 1,500
  • 17
  • 45
  • 2
    Are you sure you want that `$1` in double quotes? – choroba Jul 22 '22 at 10:45
  • I need to alternate the quotation marks - I also tried the outside quotes to be double and the $1 to be single with the same result. Is there some reason not to use double quotes for the $1? – sev Jul 22 '22 at 10:47
  • 1
    are you sure that "When I copy paste the content between the ' marks to the console this command works as expected"? – pynexj Jul 22 '22 at 10:54
  • You are right, when I copied it it had the erroneous behavior. I edited the post to invert the quotation marks and confirmed that the claimed behavior is actually happening now. – sev Jul 22 '22 at 11:00

1 Answers1

1

I was not properly escaping the $ sign in the awk command. The correct alias is:

alias gbpurge="git checkout main; git pull; git fetch --all -p; git branch -vv | grep gone | awk '{ print \$1 }' | xargs -n 1 git branch -d"

sev
  • 1,500
  • 17
  • 45