Can I get ZSH branch name completion for a `delete-branch` git alias?

The Thoughtbot dotfiles include a gitconfig which includes an alias for deleting local and remote branches. I use it all the time but it’s a real pain having to fully type out the branch name each time. I’ve tried using compdef but that just doesn’t work. I think it’s because the command is just shelling out to git maybe?

I’ve been trying to fix this on and off for months now, so any input would be awesome! This post inspired me to try again but I still can’t get it working.

Hi, Aaron… I think you could try the gbc method from this post to fill in the current branch name at the appropriate time. For example, maybe something like this could work, assuming you’re on the branch you want to delete:

 delete-branch = !sh -c 'branch=gbc && git checkout master && git push origin :refs/heads/$branch && git branch -D $branch'

I haven’t tried this out so YMMV.

There’s a PR to add auto-completion, but given some of the feedback it looks like it doesn’t work for everyone. I haven’t tried it myself, but I’m also using fish (not zsh).

Awesome thanks for that! I’ve got the git command itself returning the branches, so git delete-branch <TAB> displays the list, but now I’m trying to work out how to make my gdb zsh alias show the list too.

Thanks for the reply, but I’ve got the command itself working, I’m just trying to get zsh to add the branch names to the completion list (compdefs)

So I got it working, it seems the order matters when including custom completions, the following is a diff of my zshrc:

 # completion
+autoload -U compinit
+compinit
+
+# load our own completion functions
 fpath=(~/.zsh/completion ~/.zsh/prompts $fpath)
-autoload -U compinit && compinit
+autoload -Uz ~/.zsh/completion/*(:t)

I also don’t need to set anything to make my gdb alias work either it seems.