-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ae77a0
commit 7a97e0f
Showing
5 changed files
with
75 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,79 @@ | ||
--- | ||
title: Delete a branch | ||
type: snippet | ||
title: Delete a Git branch | ||
shortTitle: Delete branch | ||
type: story | ||
language: git | ||
tags: [repository,branch] | ||
cover: volcano-sunset | ||
dateModified: 2021-04-13 | ||
excerpt: Having trouble deleting branches in Git? Here's a guide to help you delete local, remote, detached, and merged branches. | ||
dateModified: 2024-04-06 | ||
--- | ||
|
||
Deletes a local branch. | ||
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. | ||
|
||
- Use `git branch -d <branch>` to delete the specified local `<branch>`. | ||
## Delete local branch | ||
|
||
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. | ||
|
||
```shell | ||
git branch -d <branch> | ||
# Usage: git branch -d <branch> | ||
|
||
# Examples | ||
git checkout master | ||
git branch -d patch-1 # Deletes the `patch-1` local branch | ||
``` | ||
|
||
## Delete remote 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>`. | ||
|
||
```shell | ||
# Usage: git push -d <remote> <branch> | ||
|
||
git checkout master | ||
git push -d origin patch-1 # Deletes the `patch-1` remote branch | ||
``` | ||
|
||
## Delete detached branches | ||
|
||
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. | ||
|
||
```shell | ||
# 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 | ||
``` | ||
|
||
## 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. | ||
|
||
```shell | ||
# Usage: | ||
# git branch --merged <branch> | grep -v "(^\*|<branch>)" | xargs git branch -d | ||
|
||
git checkout master | ||
git branch | ||
# master | ||
# patch-1 | ||
# patch-2 | ||
|
||
# Assuming `patch-1` is merged into master | ||
git branch --merged master | grep -v "(^\*|master)" | xargs git branch -d | ||
|
||
git branch | ||
# master | ||
# patch-2 | ||
``` |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.