LinuxCommandLibrary

git-delete-branch

Delete a Git branch

TLDR

Delete one or more local and remote Git branches

$ git delete-branch [branch_name1 branch_name2 ...]
copy

SYNOPSIS

git-delete-branch [-f | --force] <branchname>

PARAMETERS

-f, --force
    Force deletion of unmerged branch, ignoring safety checks

-r, --remotes
    Delete matching remote-tracking branches (alias equivalent)

<branchname>
    Name of the local branch to delete

DESCRIPTION

git-delete-branch is not a standard core Git command but commonly refers to an alias, script, or wrapper for git branch -d or git branch --delete, used to remove local branches from a Git repository.

It safely deletes a specified branch only if it is fully merged into its upstream branch or the current HEAD, preventing accidental loss of unmerged changes. This promotes safe branching workflows by encouraging cleanup of completed feature branches.

Usage typically involves switching to another branch first (e.g., main or master), then invoking the command. For unmerged branches, a force option allows deletion at the risk of data loss.

This command operates locally and does not affect remote repositories on platforms like GitHub or GitLab. It integrates into Git workflows for maintaining clean history, reducing repository clutter, and improving clone times for collaborators.

Best practices include verifying branch status with git branch -v before deletion and using it post-merge during release cycles.

CAVEATS

Cannot delete the current branch; switch branches first.
Local only—use git push origin --delete <branch> for remotes.
Force deletion (-f) risks losing unmerged commits.

EXAMPLE

git checkout main
git-delete-branch feature-xyz
Safely deletes if merged.

git-delete-branch -f experimental
Forces unmerged deletion.

VERIFY BEFORE DELETE

Run git branch -v to check merge status.
git log --oneline main..feature shows pending commits.

HISTORY

Branch deletion via git branch -d introduced in Git v1.5.0 (February 2007). Aliases like 'git-delete-branch' popularized in dotfiles and tools like git-extras (2010s) for user convenience.

SEE ALSO

git branch(1), git checkout(1), git push(1)

Copied to clipboard