How to Install and Use git-trim in Linux to Automatically Clean Up Merged Git Branches

Active feature branch development in Git inevitably leads to branch clutter. Once pull requests are merged, rebased, or squash-merged into the upstream trunk, local copies of those feature branches remain on your development machine unless manually deleted. While standard git branch -d deletes branches cleanly merged into HEAD, it fails to identify branches incorporated via GitHub squash-and-merge or rebasing workflows. git-trim is a specialized command-line utility that inspects upstream remote tracking branches and automatically deletes merged local branches safely.

Understanding git-trim vs Native Git Pruning

Running git fetch --prune only removes remote-tracking references (e.g., origin/feature-auth) that no longer exist on the remote server. It does not delete your local working branches (e.g., feature-auth). Furthermore, running git branch --merged relies on direct commit ancestry graphs; if your pull request was squash-merged, the commit hashes differ, causing standard Git commands to report the branch as unmerged.

The git-trim utility solves this challenge by querying GitHub, GitLab, or remote commit histories using heuristic patch-id matching and merge commit detection, identifying branches that are effectively merged even if their commit hashes changed.

Installing git-trim on Linux Distributions

The git-trim tool is written in Rust and distributed as a native binary, Cargo crate, and pre-compiled package:

# Install using Cargo (Rust package manager)
cargo install git-trim

# Install on Arch Linux via AUR
yay -S git-trim

# Download precompiled binary directly from GitHub releases
curl -sSL https://github.com/foriequal0/git-trim/releases/latest/download/git-trim-linux-x86_64.tar.gz | tar -xz
sudo mv git-trim /usr/local/bin/

Verify installation by querying the utility via the Git wrapper:

git trim --version

Running a Dry-Run Inspection

Before allowing git-trim to remove local or remote branches, execute a dry run to inspect its decision matrix without altering your repository state:

# Fetch latest upstream status and simulate branch trimming
git trim --dry-run

The output categorises your repository branches into three distinct sections:

  • Delete: Branches whose changes have been merged into the default tracking branch (main/master).
  • Hold: Branches with unmerged commits or active diverged histories.
  • Protected: Branches explicitly excluded from deletion via configuration.

Configuring Protected Branches and Base Tracking

To prevent accidental deletion of ongoing release, staging, or long-term development branches, register them in your local or global Git configuration:

# Set base branch to track against
git config trim.base main

# Set protected branch patterns (comma-separated or regex)
git config trim.protected "main,staging,develop,release-*"

# View current git-trim configurations
git config --get-regexp '^trim\.'

Trimming Local and Remote Branches Safely

When you are confident with the dry-run results, execute branch cleanup interactively:

# Interactive mode prompts for confirmation before deletion
git trim

# To delete local branches and simultaneously prune merged remote tracking branches
git trim --delete-remote

If you encounter a branch that git-trim incorrectly marks for retention due to rebase conflicts, you can inspect the exact commit difference using:

git log --oneline main..feature-branch

git-trim Command Flags and Options

Consult the summary table below for common operational flags supported by git-trim:

Command / Flag Operational Purpose Safety Impact
git trim --dry-run Simulates branch analysis without modifying working copy Non-destructive (Read-only)
git trim Interactively deletes merged local branches Prompts before deletion
git trim --no-confirm Bypasses interactive prompt for automated CI/scripts Deletes immediately
git trim --delete-remote Deletes both local and merged remote-tracking branches Pushes branch deletion to origin
git trim --update Executes git fetch --prune prior to analysis Ensures remote sync

Integrating git trim into your weekly maintenance routine keeps repository trees clean, accelerates branch navigation in terminal completions, and prevents confusing merge prompts during upstream pulls.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.