git-delete-branch
Delete a Git branch
TLDR
Delete one or more local and remote Git branches
SYNOPSIS
For local branches:git branch -d branch-name
git branch -D branch-name
For remote branches:git push remote-name --delete branch-name
(or equivalently: git push remote-name :branch-name
)
PARAMETERS
-d or --delete
Deletes the branch specified by branch-name if it has been fully merged into its upstream branch (or HEAD
if no upstream is set). This is the 'safe' way to delete a local branch.
-D
Forces the deletion of the branch specified by branch-name, regardless of its merged status. This is useful for discarding unmerged feature branches or branches that are no longer needed. Alias for --delete --force
.
--delete
When used with git push
, this option deletes the specified branch on the remote repository. It sends a command to the remote to remove the branch reference.
branch-name
The name of the local or remote branch to be deleted. This argument is mandatory for all deletion commands.
remote-name
The name of the remote repository (e.g., origin
) from which the branch should be deleted. This argument is used with git push --delete
.
DESCRIPTION
The term 'git-delete-branch' commonly refers to the process of removing branches from a Git repository. Git provides distinct commands for deleting local branches and remote branches, each with options for safe or forceful removal. For local branches, git branch -d
offers a safe deletion, only proceeding if the branch has been fully merged into its upstream or HEAD
. This prevents accidental loss of unmerged work. Conversely, git branch -D
(uppercase D) forces the deletion of a local branch, even if it contains unmerged commits. This is useful for discarding experimental or abandoned feature branches. Deleting remote branches requires interaction with the remote repository using git push --delete
. This command sends a signal to the remote to remove the specified branch. After a remote branch is deleted, you may need to prune your local remote-tracking branches to reflect the changes. It's crucial to understand these distinctions to manage your repository's branch history effectively and avoid losing valuable work.
CAVEATS
- You cannot delete the Git branch you are currently checked out on. You must first switch to another branch.
- Using
git branch -D
on an unmerged branch will permanently discard any commits unique to that branch. Ensure you no longer need the work before using this option. - Deleting a remote branch using
git push --delete
only removes the branch from the remote repository. Your local repository will still have a remote-tracking branch reference (e.g.,origin/my-feature
) until you prune it (e.g., withgit fetch --prune
orgit remote prune remote-name
). - Be cautious when deleting branches, especially remote ones, as it affects shared history and collaborators.
DELETING LOCAL BRANCHES
To remove a local branch, use git branch -d branch-name
for a safe deletion; Git will only delete it if it's fully merged into your current HEAD
or its upstream. If the branch has unmerged changes or you wish to force its removal regardless of merge status, use git branch -D branch-name
. This is common for experimental branches that are no longer relevant.
DELETING REMOTE BRANCHES
To delete a branch from a remote repository (e.g., origin
), use the command git push remote-name --delete branch-name
. For example, git push origin --delete feature/login-v1
. After this operation, the remote branch will be removed from the shared repository. Your local repository might still show a stale remote-tracking branch; to clean this up, run git fetch --prune
or git remote prune remote-name
.
HISTORY
Git's branch management, including deletion, has been a core feature since its inception. The git branch -d
and -D
options were designed to offer both cautious and forceful deletion capabilities, catering to different workflow needs. The git push --delete
syntax was later introduced to simplify the process of removing remote branches, replacing earlier, less intuitive methods like git push remote-name :branch-name
(pushing an empty ref to delete). This evolution reflects Git's continuous effort to provide more user-friendly and explicit commands for common tasks.
SEE ALSO
git branch(1), git push(1), git fetch(1), git checkout(1)