If you want to make some free space on the server or if you feel annoyed to see too many git branches on remote as you will never be using them in lifetime, then there is a solution for deleting the old git branches.
If you are using GitLab, GitHub or any other git supported repository for version controlling for your project, this solution should work for you.
We will be listing branches from oldest commit date to newest.
The command is as follows:
git for-each-ref --sort=committerdate refs/heads/ --format='%(HEAD) %(color:cyan)%(refname:short) - %(color:red)%(objectname:short)%(color:reset) - %(contents:subject) - %(authorname) (%(color:green)%(committerdate:relative)%(color:reset))'
Command should return the result which consist of Branch name, Last commit id, Description of last commit, Author, Date of the last commit.
Now you can figure out which branches you would like to delete. Prepare a list of branches you would like to delete.
In our case, we will delete Test_1 and Test_2.
Basically, you can delete as many branches as you want. There are two ways as follows:
1) git push origin -d branch-name1 branch-name2
2) git push origin -d branch-name1
git push origin -d branch-name2
Either you need to pass multiple branch names to the command, or you need to prepare a list of command followed by single branch name and paste entire list in command prompt or powershell at once.
For example:
1) git push origin -d Test_1 Test_2
2) git push origin -d Test_1
git push origin -d Test_2
We will be using the first option here. It will delete both Test_1 and Test_2 branches at once.
GIT has deleted both branches. Now let's verify that using the command earlier we used to list branches by oldest commit date first order.
Let's also check on GitHub.
We can see that both Test_1 and Test_2 branches have been deleted.
Top comments (0)