Delete old branches in Git

Sometimes there are many branches, accumulated over the years in a git repository. And you need to delete it to save some space or make management of your repository easy.

To get the list of all local branches

╭─[email protected] ~/stringify ‹master*›
╰─$ git branch
ci-build-script
dev
* master
remotes/origin/add-license-1
remotes/origin/benchmark

After that, you can iterate over all local branches and get some technical details like branch creation date(age of the branch), last commit date, last commit message etc.

I will try to delete ci-build-script.

╭─[email protected] ~/stringify ‹master*›
╰─$ git merge-base ci-build-script master
e3b47d97284b911baa266853b208f5ea25115e67

git merge-base tries to find out a good common ancestor. This can be assumed to return the earliest commit id where branch deviates from master branch.

You can use the commit id of the common ancestor to get creation date and the age of the branch.

╭─[email protected] ~/stringify ‹master*› 
╰─$ git log — pretty=format:”%ad” — date=short -n 1 e3b47d97284b911baa266853b208f5ea25115e67
2017–09–25
╭─[email protected] ~/stringify ‹master*›
╰─$ git log — pretty=format:”%cr” — date=short -n 1 e3b47d97284b911baa266853b208f5ea25115e67
6 months ago

Similarly, you can use the branch name to get the latest commit date, age of last commit and the subject line of last commit message.

╭─[email protected] ~/stringify ‹master*› 
╰─$ git log — pretty=format:”%ad” — date=short -n 1 ci-build-script
2017–09–26
╭─[email protected] ~/stringify ‹master*›
╰─$ git log — pretty=format:”%cr” — date=short -n 1 ci-build-script
5 months ago
╭─[email protected] ~/stringify ‹master*›
╰─$ git log --pretty=format:"%s" -n 1 ci-build-script
gcc tnd clang config for travis

From that you can make an intelligent guess, should you delete the branch or not. The branch ci-build-script was created 5 months ago and last committed 5 months ago. The commit message was “gcc and clang config for travis”.

You can delete it

git branch -d ci-build-script

-d option won’t allow you delete unless it is merged. In some cases branch were never merged. So, you can use -D option

You can wrap all the above and make a script.

The script has 2 options: -i and -f-.

If the run the script without any options, it will give you list of all local branches and the above information.

╰─$ git-clean-local-branches 
Branch name : ci-build-script
Created on : 2017–09–25(6 months ago)
Last updated on : 2017–09–26(5 months ago)
Last commit message : gcc tnd clang config for travis
Branch name : dev
Created on : 2017–10–14(5 months ago)
Last updated on : 2017–11–25(4 months ago)
Last commit message : stringify improvement, size and type name removal
Branch name : remotes/origin/add-license-1
Created on : 2017–10–14(5 months ago)
Last updated on : 2017–10–14(5 months ago)
Last commit message : Create LICENSE
Branch name : remotes/origin/benchmark
Created on : 2017–10–06(5 months ago)
Last updated on : 2017–10–16(5 months ago)
Last commit message : cxx-pretty print integration

-i option allows you interactive deletion. -f option uses git delete -D(force delete).

╭─[email protected] ~/stringify ‹master*› 
╰─$ git-clean-local-branches -fi
Branch name : ci-build-script
Created on : 2017–09–25(6 months ago)
Last updated on : 2017–09–26(5 months ago)
Last commit message : gcc tnd clang config for travis
Delete the branch, followed by [y/n]? y
Deleted branch ci-build-script (was 2cb728c).
Branch name : dev
Created on : 2017–10–14(5 months ago)
Last updated on : 2017–11–25(4 months ago)
Last commit message : stringify improvement, size and type name removal
Delete the branch, followed by [y/n]? y
Deleted branch dev (was f8698b4).

Thanks for reading.


Delete old branches in Git was originally published in Hacker Noon on Medium, where people are continuing the conversation by highlighting and responding to this story.

read original article here