DEV Community

Alexander Budchanov
Alexander Budchanov

Posted on • Originally published at jetrockets.pro

Keep clean your git repos!

Many developers don't keep their local repositories clean. There is a way how to automate it though. Let's look at a few useful commands:

To clean refs to nonexistent branches in the remote:

$ git fetch --prune
Enter fullscreen mode Exit fullscreen mode

--prune before fetching, remove any remote-tracking references that no longer exist on the remote.

To estimate how many branches merged into dev:

$ git branch --merged dev | wc -l
Enter fullscreen mode Exit fullscreen mode

--merged option can filter list to branches that you have merged into the given branch. Squash and rebase merges usually aren't detected by --merged.

List of branches merged into dev:

$ git branch --merged dev
Enter fullscreen mode Exit fullscreen mode

List of remote branches merged into dev:

$ git branch --merged dev --remote
Enter fullscreen mode Exit fullscreen mode

If you are courageous then:

$ git branch --merged dev | egrep -v "(^\*|master|dev)" | xargs git branch -d
Enter fullscreen mode Exit fullscreen mode

It removes all local branches that merged into dev (except dev and master).
This is a potentially damaging operation. It can delete branches actually needed.
So if you use the different approach to work with Git, you could remove some of branches manually instead. I hope you do not store all old branches, do you?

Top comments (0)