Automating GitHub Cleanup: A Bash Script for Git Repository Management

Automating GitHub Cleanup: A Bash Script for Git Repository Management


Welcome to this week's BashFriday project! In this project, we'll be exploring how to create a powerful Git repository cleaner script using Bash scripting. Our script is designed to help you keep your Git repositories organized and clutter-free by performing three main tasks: Branch Cleanup, Tag Cleanup, and Repository Cleanup.


The primary objectives of this project are as follows:

  1. Branch Cleanup: Identify and remove stale branches in Git repositories. Stale branches are those that are no longer needed or have been merged into the main branch.

  2. Tag Cleanup: Identify and remove stale tags (e.g., release tags) in Git repositories. Stale tags are those that are no longer relevant.

  3. Repository Cleanup: Identify and archive or delete repositories that are no longer actively used based on custom criteria such as last commit date, activity, and more.


Our Git Repository Cleaner script offers the following features:

1. Automated Branch Cleanup

  • Lists all branches in each repository.

  • Checks each branch's status, including the last commit date.

  • Identifies stale branches that can be safely deleted.

  • Prompts the user for branch cleanup.

3. Repository Cleanup

  • Lists all repositories in your Git organization.

  • Checks each repository's last commit date.

  • Checks for protection rules and open pull requests.

  • Identifies repositories that can be archived or deleted based on custom criteria.

4. Integration with GitHub API

  • Uses a Personal Access Token (PAT) for authentication.

  • Integrates with the GitHub API to fetch repository data.

5. User-Friendly Prompts

  • Prompts the user for cleanup actions, ensuring safe and controlled operations.


To use the Git Repository Cleaner script, follow these steps:

  1. Set up a Personal Access Token (PAT) on your Git hosting service (e.g., GitHub) with the required scopes.

  2. Replace the GIT_TOKEN, GIT_HOST, and USERNAME variables with your PAT and relevant information.

  3. Run the script to start the cleanup process.



Maintaining clean and organized Git repositories is crucial for efficient development and collaboration. With our Git Repository Cleaner script, you can automate the process of identifying and removing stale branches, tags, and repositories, ensuring that your version control system remains tidy and efficient.

Happy cleaning, and enjoy your clutter-free Git repositories!