Skip to content

Commit

Permalink
Merge branch deletion snippets
Browse files Browse the repository at this point in the history
  • Loading branch information
Chalarangelo committed Apr 14, 2024
1 parent 0ae77a0 commit 7a97e0f
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 90 deletions.
9 changes: 9 additions & 0 deletions content/redirects.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2812,3 +2812,12 @@
- from: /git/s/save-stash
to: /git/s/stashing
status: 301!
- from: /git/s/delete-detached-branches
to: /git/s/delete-branch
status: 301!
- from: /git/s/delete-merged-branches
to: /git/s/delete-branch
status: 301!
- from: /git/s/delete-remote-branch
to: /git/s/delete-branch
status: 301!
73 changes: 66 additions & 7 deletions content/snippets/git/s/delete-branch.md
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
```
31 changes: 0 additions & 31 deletions content/snippets/git/s/delete-detached-branches.md

This file was deleted.

32 changes: 0 additions & 32 deletions content/snippets/git/s/delete-merged-branches.md

This file was deleted.

20 changes: 0 additions & 20 deletions content/snippets/git/s/delete-remote-branch.md

This file was deleted.

0 comments on commit 7a97e0f

Please sign in to comment.