diff --git a/snippets/delete-branch.md b/snippets/delete-branch.md index ea7b85aa1..ddf85306d 100644 --- a/snippets/delete-branch.md +++ b/snippets/delete-branch.md @@ -5,7 +5,7 @@ tags: repository,branch,beginner Deletes a local branch. -- Use `git branch -d ` to delete the branch with the specified branch name. +- Use `git branch -d ` to delete the specified local ``. ```sh git branch -d diff --git a/snippets/delete-detached-branches.md b/snippets/delete-detached-branches.md new file mode 100644 index 000000000..bcf24a33f --- /dev/null +++ b/snippets/delete-detached-branches.md @@ -0,0 +1,28 @@ +--- +title: Delete detached branches +tags: repository,branch,intermediate +--- + +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. + +```sh +git fetch --all --prune +``` + +```sh +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/snippets/delete-merged-branches.md b/snippets/delete-merged-branches.md new file mode 100644 index 000000000..a338ddfaf --- /dev/null +++ b/snippets/delete-merged-branches.md @@ -0,0 +1,29 @@ +--- +title: Delete merged branches +tags: repository,branch,advanced +--- + +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. + +```sh +git branch --merged | grep -v "(^\*|)" | xargs git branch -d +``` + +```sh +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/snippets/delete-remote-branch.md b/snippets/delete-remote-branch.md new file mode 100644 index 000000000..dec8164b1 --- /dev/null +++ b/snippets/delete-remote-branch.md @@ -0,0 +1,17 @@ +--- +title: Delete a remote branch +tags: repository,branch,intermediate +--- + +Deletes a remote branch. + +- Use `git push -d ` to delete the specified remote `` on the given ``. + +```sh +git push -d +``` + +```sh +git checkout master +git push -d origin patch-1 # Deletes the `patch-1` remote branch +```