Smarter branch deletion: Git alias 'del'

14
February, 2024

This Git alias "del" efficiently deletes a local branch while ensuring it's up-to-date with the main (or master) branch. It automatically switches to the main branch, fetches updates from the remote, pulls changes, and deletes the specified branch.

You may add this alias to your git config file located at /Users/your_username/.gitconfig

[alias]
  del = "!f() { \
    git rev-parse --verify main >/dev/null 2>&1 && branch=main || branch=master; \
    git checkout $branch; \
    git fetch -p; \
    git pull; \
    git branch -D \"$@\"; \
  }; f"