DEV Community

Clarice Bouwer
Clarice Bouwer

Posted on

How to clean up branches in Git

To delete branches already merged:

git branch --merged | grep -v '^[ *]*main$' | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

Where main is trunk commonly master or main.

If you are looking to do a deep clean where branches are not merged then you can use the D switch instead of d.

Reference @ Stack Overflow

Have you made a mistake? Recover your branch by getting it's SHA and checking it out.

git reflog --no-abbrev
git checkout -b <your-branch> <sha>
Enter fullscreen mode Exit fullscreen mode

Reference @ Stack Overflow

Top comments (0)

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