paint-brush
Delete old branches in Gitby@asitdhal
4,326 reads
4,326 reads

Delete old branches in Git

by Asit DhalMarch 15th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Sometimes there are many <a href="https://hackernoon.com/tagged/branches" target="_blank">branches</a>, accumulated over the years in a <a href="https://hackernoon.com/tagged/git" target="_blank">git</a> repository. And you need to delete it to save some space or make management of your repository easy.
featured image - Delete old branches in Git
Asit Dhal HackerNoon profile picture

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







╭─asit@gandua ~/stringify ‹master*›╰─$ git branchci-build-scriptdev* masterremotes/origin/add-license-1remotes/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.



╭─asit@gandua ~/stringify ‹master*›╰─$ git merge-base ci-build-script mastere3b47d97284b911baa266853b208f5ea25115e67

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.






╭─asit@gandua ~/stringify ‹master*›╰─$ git log — pretty=format:”%ad” — date=short -n 1 e3b47d97284b911baa266853b208f5ea25115e672017–09–25╭─asit@gandua ~/stringify ‹master*›╰─$ git log — pretty=format:”%cr” — date=short -n 1 e3b47d97284b911baa266853b208f5ea25115e676 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.









╭─asit@gandua ~/stringify ‹master*›╰─$ git log — pretty=format:”%ad” — date=short -n 1 ci-build-script2017–09–26╭─asit@gandua ~/stringify ‹master*›╰─$ git log — pretty=format:”%cr” — date=short -n 1 ci-build-script5 months ago╭─asit@gandua ~/stringify ‹master*›╰─$ git log --pretty=format:"%s" -n 1 ci-build-scriptgcc 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-branchesBranch name : ci-build-scriptCreated on : 2017–09–25(6 months ago)Last updated on : 2017–09–26(5 months ago)Last commit message : gcc tnd clang config for travisBranch name : devCreated 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 removalBranch name : remotes/origin/add-license-1Created on : 2017–10–14(5 months ago)Last updated on : 2017–10–14(5 months ago)Last commit message : Create LICENSEBranch name : remotes/origin/benchmarkCreated 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).














╭─asit@gandua ~/stringify ‹master*›╰─$ git-clean-local-branches -fiBranch name : ci-build-scriptCreated on : 2017–09–25(6 months ago)Last updated on : 2017–09–26(5 months ago)Last commit message : gcc tnd clang config for travisDelete the branch, followed by [y/n]? yDeleted branch ci-build-script (was 2cb728c).Branch name : devCreated 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 removalDelete the branch, followed by [y/n]? yDeleted branch dev (was f8698b4).

Thanks for reading.