gitparametersalias

git alias for deleting branches by given pattern


After working for a few weeks, I always accumulate a lot of branches, frequently called feature/* or daniel/*.

From time to time, I delete all my local branches running this command (windows):

git branch -d (git branch --list 'feature/*').trim()

After frequently forgetting this command, I'd like to create an alias for it (with a string argument) ideally like this:

git del 'feature/*'

How can I create this alias?


I was trying this:

[alias]

    del = '!set -eu; git branch -d (git branch --list "$1").trim()'

but it doesn't work and I ran out of ideas.


Solution

  • Instead of nesting commands, you could pipe them and retrieve the result of your git branch --list "$1" to feed git branch -d with xargs -r. In order to expand an alias with piped commands, this needs to start with an exclamation mark. Your alias could look like this:

    git config --local alias.del '!set -eu | git branch --list "$1" | xargs -r git branch -d'
    

    and use it like so:

    # deleting all branches starting with feature/
    git del feature/*
    
    # deleting all branches starting with daniele/
    git del daniel/*