HiveBrain v1.2.0
Get Started
← Back to all entries
snippetjavascriptTip

Delete a Git branch

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
branchdeletegit

Problem

Branches are an essential part of Git, allowing you to split up development work and manage different features or bug fixes. However, as your project progresses, you may accumulate branches that are no longer needed. Deleting these branches can help keep your repository clean and organized.
In order to delete a local branch, you can use the git branch -d <branch> command. This command deletes the specified local <branch>. Note that you need to switch to a different branch before deleting the target branch. Remember that, if the branch has a remote counterpart, you have to delete the remote branch as well.
Similar to deleting a local branch, you can delete a remote branch using the git push -d <remote> <branch> command. This command deletes the specified remote <branch> on the given <remote>.
Detached branches are branches that are not associated with any commit. They are often created when you check out a specific commit or tag. You can use the git fetch --all --prune command garbage collect any detached branches.
This command is especially useful if the remote repository is set to automatically delete merged branches.

Solution

# Usage: git branch -d <branch>

git checkout master
git branch -d patch-1 # Deletes the `patch-1` local branch


Similar to deleting a local branch, you can delete a remote branch using the git push -d <remote> <branch> command. This command deletes the specified remote <branch> on the given <remote>.
Detached branches are branches that are not associated with any commit. They are often created when you check out a specific commit or tag. You can use the git fetch --all --prune command garbage collect any detached branches.
This command is especially useful if the remote repository is set to automatically delete merged branches.
You can use git branch --merged <branch> to list all branches merged into the target <branch> (e.g. master). Then, you can use the pipe operator (|) to pipe the output and grep -v "(^\*|<branch>)" to exclude the current and the target <branch>. Finally, use the pipe operator (|) to pipe the output and xargs git branch -d to delete all of the found branches.

Code Snippets

# Usage: git branch -d <branch>

git checkout master
git branch -d patch-1 # Deletes the `patch-1` local branch
# Usage: git push -d <remote> <branch>

git checkout master
git push -d origin patch-1 # Deletes the `patch-1` remote branch
# Usage: git fetch --all --prune

git checkout master
git branch
# master
# patch-1
# patch-2

# Assuming `patch-1` is detached
git fetch --all --prune

git branch
# master
# patch-2

Context

From 30-seconds-of-code: delete-branch

Revisions (0)

No revisions yet.