Fast Way to Clean Up Short-Lived Git Feature Branches

Tuesday, April 2, 2024

When using a feature-branch workflow in Git, where each new feature or bug fix is developed in its own branch, the number of short-lived branches accumulates fast, both locally and remotely. Managing and cleaning up these branches can become a tedious task.

Pruning commands

Typically, developers might use a combination of git fetch --prune and git branch -d commands to clean up their local repositories. git fetch --prune updates your remote-tracking branches and removes references to remote branches that have been deleted. Meanwhile, git branch -d allows for the deletion of branches that have been merged into the mainline, ensuring that work is not lost inadvertently.

Scripting

While effective, performing these tasks manually can be time-consuming, especially in active repositories with frequent merges and deletions. For those looking to streamline this process, automation through shell scripts has been a go-to solution. However, there's an even simpler and more efficient solution.

Git-removed-branches

Enter git-removed-branches, a CLI tool designed specifically for the task of listing or removing local tracked branches that are no longer present on the remote. This tool simplifies the workflow to a single command, automating what would otherwise be a manual and error-prone process.

Features of git-removed-branches:

  • List Removed Branches: Quickly identify which local branches are tracking remote branches that have been deleted.
  • Remove Locally Tracked Branches: Safely delete these branches from your local repository with a single command.
  • Streamlined Workflow: Incorporate this tool into your development routine to keep your repository clean and manageable.

Getting Started:

Installation is straightforward via npm.

npm install -g git-removed-branches

After installation, you can use it as a plain git command.

To list branches that have been removed from the remote but are still present locally, simply run:

git removed-branches --list

And to remove them:

git removed-branches --prune

You can also use the tool without installing it first with npx:

npx git-removed-branches [parameters]

By integrating git-removed-branches into your Git workflow, you can significantly reduce the clutter in your local repository and maintain a more organized, efficient development environment.