LinuxCommandLibrary

git-delete-merged-branches

Delete branches already merged into the current branch

TLDR

Delete merged branches

$ git delete-merged-branches
copy

SYNOPSIS

git-delete-merged-branches [options] [target_branch]

PARAMETERS

-r, --remote
    Considers remote-tracking branches in the deletion process, often ensuring local branches that track merged remote branches are cleaned up.

-f, --force
    Force deletion of branches using git branch -D. This allows deletion of unmerged branches or branches whose upstream hasn't been pushed.

-n, --dry-run
    Performs a dry run, listing branches that would be deleted without actually deleting them. Highly recommended for previewing.

-i, --interactive
    Prompts for confirmation before deleting each branch, providing an opportunity to review and skip individual branches.

-k, --keep
    Specifies a pattern (e.g., glob) for branches to explicitly keep and exclude from deletion, even if they are merged.


    The branch against which to check for merged branches. If omitted, defaults to the currently checked-out branch (HEAD).

DESCRIPTION

The git-delete-merged-branches command is a common utility script or alias used to clean up local Git repositories by removing branches that have already been fully merged into the current HEAD branch (or a specified target branch). It automates the process of identifying merged branches and then deleting them, helping to keep the local branch list tidy and manageable. Typically, it identifies branches using git branch --merged and then deletes them using git branch -d or git branch -D (force delete). It often includes safeguards to prevent accidental deletion of important branches like master, main, or develop, and might offer interactive confirmation. This command is not a built-in Git command but rather a highly popular user-created script or function built upon Git's core functionalities. Its primary purpose is to streamline repository hygiene, especially in workflows with many short-lived feature branches.

CAVEATS

  • Non-standard Command: This is not a built-in git command. Its exact behavior depends on the specific script or alias implementation you are using. Always verify the source of the script.

  • Potential Data Loss: Without careful use of --dry-run or --interactive, branches can be permanently deleted, especially if --force is used or the branch has not been pushed to a remote repository.

  • Contextual Merged State: A branch might be merged into your current HEAD, but not necessarily into a long-lived branch like main or develop. Ensure you understand which target_branch is being used for the merge check.

  • Local Only: Typically, this command only deletes local branches. Deleting remote branches requires a separate push command (e.g., git push origin --delete <branch_name>).

  • Active Branch Protection: Most implementations prevent deleting the currently checked-out branch.

INSTALLATION/SETUP


This command needs to be manually set up as it is not a built-in Git command. It can be implemented as a shell script placed in your $PATH or as a Git alias configured in your .gitconfig file.

Example as a basic Git alias:
git config --global alias.delmerged '!git branch --merged | grep -v "master\\|main\\|develop" | xargs git branch -d'

More robust scripts often include error handling, interactivity, and more sophisticated branch exclusion logic.

WORKFLOW INTEGRATION


This command is often integrated into automated workflows, such as post-merge hooks in Git or as part of CI/CD pipeline steps. This ensures that local developer environments or build servers remain clean of stale branches after successful merges to integration branches.

HISTORY

The git-delete-merged-branches utility does not have a formal, centralized history like built-in Git commands. Its origin stems from the common need for efficient repository hygiene as Git gained widespread adoption. Developers frequently create numerous short-lived feature or bugfix branches, and manually identifying and deleting these after they were merged became a tedious task. This practical necessity led to the proliferation of custom shell scripts and aliases shared within the developer community (e.g., via GitHub Gists, Stack Overflow, and personal dotfiles). While many variations exist, the core logic leveraging git branch --merged for identification and git branch -d for deletion rapidly became a de facto standard for such cleanup utilities. Its evolution is primarily driven by community contributions, practical workflow requirements, and individual preferences for automation rather than a centralized development effort.

SEE ALSO

Copied to clipboard