DEV Community

Temuri Takalandze
Temuri Takalandze

Posted on

4

🚀 Clean Up Your Local Git Branches! 🧹

Image description

Over time, local branches pile up, especially after merging or deleting them remotely. To remove local branches that no longer have a remote counterpart, follow these simple steps:

1️⃣ List branches that are gone from the remote:
👉 git branch -vv | awk '/: gone]/{print $1}'

3️⃣ Delete them:
👉 git branch -vv | awk '/: gone]/{print $1}' | xargs git branch -d

⚠️ If Git complains that a branch is not fully merged, use -D instead of -d, but be careful! 🚨

Keep your workspace clean & organized! ✅

Top comments (4)

Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
larastewart_engdev profile image
Lara Stewart - DevOps Cloud Engineer

The idea is good, but would you run a few commands to ensure that no important local branches are deleted?

If you have only a few branches, which will be the case 99% of the time, you might never need it. However, if your Git branches are completely cluttered with unnecessary ones, this could come in handy.

Collapse
 
marlonlom profile image
Marlon López • Edited

Hint:
Make the git command an alias:
alias gitgone="git fetch -p && git branch -vv | grep 'origin/.*: gone]' | awk '{print \$1}' | xargs git branch -D"

Collapse
 
abgeo profile image
Temuri Takalandze

@marlonlom You can even create a Git alias instead of a system one and use it like this:
git config --global alias.gone '!git branch -vv | awk "/: gone]/{print }"'
git gone

Some comments have been hidden by the post's author - find out more

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay