Skip to content

A cross-platform Git utility to automatically clean up local branches that no longer exist on remote. Supports both Unix-like systems (Linux/macOS) and Windows. Features include interactive confirmation, colored output, and safety checks.

License

Notifications You must be signed in to change notification settings

thiengo105/git-cleanup-branch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Git Cleanup Branches

A cross-platform tool to clean up local Git branches that no longer exist on the remote repository. Available for both Unix-like systems (Linux/macOS) and Windows.

Features

  • Automatically detects and lists local branches that no longer exist on remote
  • Prevents deletion of the current branch
  • Interactive confirmation before deletion
  • Option to skip confirmation
  • Colored output for better visibility
  • Cross-platform support (Bash and PowerShell)

Installation

Unix-like Systems (Linux/macOS)

  1. Download the bash script directly:

    # Using curl
    curl -O https://raw.githubusercontent.com/thiengo105/git-cleanup-branch/main/git-cleanup-branches
    
    # Or using wget
    wget https://raw.githubusercontent.com/thiengo105/git-cleanup-branch/main/git-cleanup-branches
  2. Make the script executable:

    chmod +x git-cleanup-branches
  3. Move the script to a directory in your PATH:

    sudo mv git-cleanup-branches /usr/local/bin/

Windows

  1. Download the PowerShell script directly:

    # Using PowerShell
    Invoke-WebRequest -Uri "https://raw.githubusercontent.com/thiengo105/git-cleanup-branch/main/git-cleanup-branches.ps1" -OutFile "git-cleanup-branches.ps1"
  2. Create a PowerShell profile if you don't have one:

    if (!(Test-Path -Path $PROFILE)) {
        New-Item -ItemType File -Path $PROFILE -Force
    }
  3. Add the script to a directory in your PowerShell PATH (recommended location):

    # Create the directory if it doesn't exist
    New-Item -ItemType Directory -Force -Path "$HOME\Documents\WindowsPowerShell\Scripts"
    # Move the script
    Move-Item -Path "git-cleanup-branches.ps1" -Destination "$HOME\Documents\WindowsPowerShell\Scripts"
  4. Add an alias to your PowerShell profile:

    Add-Content -Path $PROFILE -Value "Set-Alias -Name git-cleanup-branches -Value `"$HOME\Documents\WindowsPowerShell\Scripts\git-cleanup-branches.ps1`""

Alternative Installation Methods

Using Git Clone

If you prefer to clone the entire repository:

# Clone the repository
git clone https://github.com/thiengo105/git-cleanup-branch.git
cd git-cleanup-branch

# For Unix-like systems
chmod +x git-cleanup-branches
sudo mv git-cleanup-branches /usr/local/bin/

# For Windows
# Follow steps 2-4 from the Windows installation above

Usage

Basic Usage

# Unix-like systems
git-cleanup-branches

# Windows
git-cleanup-branches

Skip Confirmation

# Unix-like systems
git-cleanup-branches -y

# Windows
git-cleanup-branches -y

How It Works

  1. The script first checks if you're in a Git repository
  2. Fetches and prunes remote-tracking branches
  3. Identifies local branches that no longer exist on remote
  4. Shows a list of branches to be deleted
  5. Prevents deletion if you're on a branch that would be deleted
  6. Asks for confirmation (unless -y flag is used)
  7. Deletes the branches and shows a summary

Safety Features

  • Prevents deletion of the current branch
  • Requires confirmation before deletion
  • Shows clear warning messages
  • Handles errors gracefully

Requirements

  • Git
  • For Windows: PowerShell 5.1 or later
  • For Unix-like systems: Bash shell

Contributing

Feel free to submit issues and enhancement requests!

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A cross-platform Git utility to automatically clean up local branches that no longer exist on remote. Supports both Unix-like systems (Linux/macOS) and Windows. Features include interactive confirmation, colored output, and safety checks.

Topics

Resources

License

Stars

Watchers

Forks