-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelete-branch.sh
More file actions
32 lines (27 loc) · 871 Bytes
/
delete-branch.sh
File metadata and controls
32 lines (27 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#!/bin/bash
# Deletes a branch locally and on the remote (note: this assumes 'main' is your main branch, not 'master')
# Params:
# branch name
# remote name (origin by default)
function dbranch() {
if [[ ! -d .git ]]; then
echo "Error: you are not in a git repo root" &&
return
fi
if [[ "$#" -ne 1 && "$#" -ne 2 ]]; then
echo "Error: you must supply the branch name and optionally the remote name (origin by default)" &&
return
fi
if [[ $1 == $(git branch --show-current) ]]; then
echo "You are tyring to delete the current branch. Switching to main..." &&
git checkout main
fi
# Delete local branch
git branch -D "$1"
# Delete the remote branch
if [ "$#" -ne 1 ]; then
git push "$2" --delete "$1"
else
git push origin --delete "$1"
fi
}