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.

    To expand an alias with parameterized commands piped together, you could use the sh command and supply it with a string containing the command list.

    Your alias could look like this

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

    and be used in the following way

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