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)