From 7a97e0f815046534e6ce6fbc3086c14d7a430b9b Mon Sep 17 00:00:00 2001 From: Angelos Chalaris Date: Sun, 14 Apr 2024 18:07:45 +0300 Subject: [PATCH] Merge branch deletion snippets --- content/redirects.yaml | 9 +++ content/snippets/git/s/delete-branch.md | 73 +++++++++++++++++-- .../git/s/delete-detached-branches.md | 31 -------- .../snippets/git/s/delete-merged-branches.md | 32 -------- .../snippets/git/s/delete-remote-branch.md | 20 ----- 5 files changed, 75 insertions(+), 90 deletions(-) delete mode 100644 content/snippets/git/s/delete-detached-branches.md delete mode 100644 content/snippets/git/s/delete-merged-branches.md delete mode 100644 content/snippets/git/s/delete-remote-branch.md diff --git a/content/redirects.yaml b/content/redirects.yaml index b6388ca5bc3..b71e4f68c9b 100644 --- a/content/redirects.yaml +++ b/content/redirects.yaml @@ -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! diff --git a/content/snippets/git/s/delete-branch.md b/content/snippets/git/s/delete-branch.md index cfb4bb3dd43..29f50b8c4b5 100644 --- a/content/snippets/git/s/delete-branch.md +++ b/content/snippets/git/s/delete-branch.md @@ -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 ` to delete the specified local ``. +## Delete local branch + +In order to **delete a local branch**, you can use the `git branch -d ` command. This command deletes the specified local ``. 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 +# Usage: git branch -d -# 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 ` command. This command deletes the specified remote `` on the given ``. + +```shell +# Usage: git push -d + +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 ` to **list all branches merged into the target** `` (e.g. `master`). Then, you can use the pipe operator (`|`) to **pipe the output** and `grep -v "(^\*|)"` to exclude the current and the target ``. 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 | grep -v "(^\*|)" | 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 +``` diff --git a/content/snippets/git/s/delete-detached-branches.md b/content/snippets/git/s/delete-detached-branches.md deleted file mode 100644 index 7a973799456..00000000000 --- a/content/snippets/git/s/delete-detached-branches.md +++ /dev/null @@ -1,31 +0,0 @@ ---- -title: Delete detached branches -type: snippet -language: git -tags: [repository,branch] -cover: brown-bird -dateModified: 2021-04-13 ---- - -Deletes all detached branches. - -- Use `git fetch --all --prune` to garbage collect any detached branches. -- This is especially useful if the remote repository is set to automatically delete merged branches. - -```shell -git fetch --all --prune - -# Examples -git checkout master -git branch -# master -# patch-1 -# patch-2 - -# Assuming `patch-1` is detached -git fetch --all --prune - -git branch -# master -# patch-2 -``` diff --git a/content/snippets/git/s/delete-merged-branches.md b/content/snippets/git/s/delete-merged-branches.md deleted file mode 100644 index ef6b2a8a132..00000000000 --- a/content/snippets/git/s/delete-merged-branches.md +++ /dev/null @@ -1,32 +0,0 @@ ---- -title: Delete merged branches -type: snippet -language: git -tags: [repository,branch] -cover: new-york-skyline -dateModified: 2021-04-13 ---- - -Deletes all local merged branches. - -- Use `git branch --merged ` to list all branches merged into ``. -- Use the pipe operator (`|`) to pipe the output and `grep -v "(^\*|)"` to exclude the current and the target ``. -- Use the pipe operator (`|`) to pipe the output and `xargs git branch -d` to delete all of the found branches. - -```shell -git branch --merged | grep -v "(^\*|)" | xargs git branch -d - -# Examples -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 -``` diff --git a/content/snippets/git/s/delete-remote-branch.md b/content/snippets/git/s/delete-remote-branch.md deleted file mode 100644 index 6b656c280b8..00000000000 --- a/content/snippets/git/s/delete-remote-branch.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -title: Delete a remote branch -type: snippet -language: git -tags: [repository,branch] -cover: tranquil-desktop -dateModified: 2021-04-13 ---- - -Deletes a remote branch. - -- Use `git push -d ` to delete the specified remote `` on the given ``. - -```shell -git push -d - -# Examples -git checkout master -git push -d origin patch-1 # Deletes the `patch-1` remote branch -```