DEV Community

Cover image for A Time-Saving Git Alias - "git sweep"
Colin Kiama
Colin Kiama

Posted on • Originally published at colinkiama.com

7 2

A Time-Saving Git Alias - "git sweep"

I never remember the command for cleaning my remote branches so I decided to create an alias for them.

The alias

git sweep
Enter fullscreen mode Exit fullscreen mode

This is a shortcut for the git fetch --prune command and a script that finds branches marked as gone then deletes all of them. There's more detail about the process here: https://www.erikschierboom.com/2020/02/17/cleaning-up-local-git-branches-deleted-on-a-remote/

Setting the alias

You can use set them using the git config command or edit the git config file directly.

Here's the command to set the alias with the git config command:

git config --global alias.sweep "! git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '\$2 == \"[gone]\" {print \$1}' | xargs -r git branch -D"
Enter fullscreen mode Exit fullscreen mode

Here's what the config file should look like with the alias added:

[alias]
sweep = ! "git fetch -p && git for-each-ref --format '%(refname:short) %(upstream:track)' | awk '$2 == \"[gone]\" {print $1}' | xargs -r git branch -D"
Enter fullscreen mode Exit fullscreen mode

Special Thanks

Thank you Erik Schierboom for providing the solution. Hopefully, this gets added directly into git one day. This alias is super useful for maintaining your repositories.

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

While many AI coding tools operate as simple command-response systems, Qodo Gen 1.0 represents the next generation: autonomous, multi-step problem-solving agents that work alongside you.

Read full post

Top comments (2)

Collapse
 
ehrbhein profile image
Irvin Gil

this was a handy snippet. Thank you.

Collapse
 
colinkiama profile image
Colin Kiama

I'm glad that you found it useful :D

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post