DEV Community

Temuri Takalandze
Temuri Takalandze

Posted on

🚀 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 (2)

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"