Question

How to delete old remote git branches via git cli or a bash script?

Posted September 15, 2020 431 views
Linux BasicsServer OptimizationGitLinux Commands

Hi git gurus!

I am supporting a few projects on GitHub, I’m at a point where I have hundreds of branches across all projects and deleting the branches manually is not really an option.

Does anyone have a script on hand to delete all merged branches which do not have any new commits since a specific date?

Any help will be appreciated!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
2 answers

Hi there @bitmap,

I had a similar case recently. Here are a couple of Bash scripts that might help you.

Delete remote inactive git branches since a specific date

The following script as it is can be used to delete old branches that have been merged and had no activity since a specific date:

#!/bin/bash

##
# Script to delete remote git branches
##

# Fetch the remote resources
git fetch

# Loop through all remote merged branches
for branch in $(git branch -r --merged | grep -v HEAD | grep -v develop | grep -v master | grep -v master | sed /\*/d); do
        if [ -z "$(git log -1 --since='Jun 15, 2020' -s ${branch})" ]; then
                echo -e `git show --format="%ci %cr %an" ${branch} | head -n 1` \\t$branch
                remote_branch=$(echo ${branch} | sed 's#origin/##' )
                # To delete the branches uncomment the bellow git delete command
                #git push origin --delete ${remote_branch}
        fi
done

A quick rundown of the script:

  • First we fetch all of the latest changes with the git fetch command
  • Then with a for loop we go through all remote branches
  • You can delete the --merged flag so that the script would delete all branches no matter if merged or not
  • The --since='Jun 15, 2020' date indicates the date since the branch has been last worked on. The above exmaple will delete all branches that have been idle (eg. no commits or any other activity) since Jun 15, 2020. Update it with the date that matches your needs.

Note that the date needs to match the above format

The script as it is will only give you a list of the branches, to make it actually delete those branches, uncomment the #git push origin --delete ${remote_branch} line by deleting the first # sign.

Delete remote branches for multiple projects

In case you are managing multiple projects, what you could do is add a list of your projects in a file called projects.txt for example and put a list of your projects, each on a new line.

The file needs to be in the same directory as the Bash script.

#!/bin/bash

##
# Script to delete remote git branches for multiple projects
##

# Loop through all specified services

for project_name in $(cat projects.txt); do

        cd ~/path/to/your/repos/directory/${project_name}
                # Fetch the remote resources
        git fetch

        # Print the project name
        echo "____ Start ${project_name} ____"

        # loop through all remote merged branches
        for branch in $(git branch -r --merged | grep -v HEAD | grep -v develop | grep -v master | grep -v main | sed /\*/d); do
                if [ -z "$(git log -1 --since='Jun 15, 2020' -s ${branch})" ]; then
                        echo -e `git show --format="%ci %cr %an" ${branch} | head -n 1` \\t$branch
                        remote_branch=$(echo ${branch} | sed 's#origin/##' )
                        # To delete the branches uncomment the bellow git delete command
                        #git push origin --delete ${remote_branch}
                fi
        done

        echo "____ End ${project_name} ____"
done

The script is quite similar to the one above but includes an extra for loop which would loop through all of the projects that you’ve specified in the projects.txt file.

Conclusion

Make sure to review the scripts and run them first without the git push origin --delete command to make sure that you are not going to delete any important branches.

For more information about git make sure to checkout the following tutorials:

DigitalOcean Git Tutorials

Top 18 Git commands that you should know

Hope that this helps!

Regards,
Bobby

Deleting remote branches
To delete a remote branch, you can’t use the git branch command. Instead, use the git push command with –delete flag, followed by the name of the branch you want to delete. You also need to specify the remote name (origin in this case) after git push.

123456789
git branch -a

*master

test

remote/origin/master

remote/origin/test

git push origin –delete test

To <URL of your repository>.git

- [deleted] test

Submit an Answer