-
Notifications
You must be signed in to change notification settings - Fork 3
/
git-br
executable file
·57 lines (50 loc) · 1.09 KB
/
git-br
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
set -e
is_force=false
while (( $# > 0 ))
do
case "$1" in
-f)
is_force=true
;;
-s|--sync)
git fetch --prune --all
git branch --merged \
| grep -v '^*' \
| xargs git branch -d
exit $?
;;
esac
shift
done
branch="$(
git branch --color -a \
| grep -v '*' \
| sed -E 's/^ //' \
| fzy \
| perl -pe 's/\e\[?.*?[\@-~]//g'
)"
if [[ -z $branch ]]; then
exit 0
fi
is_already_branched() { git branch | grep -q "${branch##*/}" &>/dev/null; }
if [[ $branch =~ ^remotes ]]; then
if is_already_branched; then
# already exists
if $is_force; then
git checkout -q master
git branch -D "${branch##*/}"
git checkout -b "${branch##*/}" "${branch#*/}"
else
git checkout "${branch##*/}"
fi
else
git checkout -b "${branch##*/}" "${branch#*/}"
fi
else
if is_already_branched; then
git checkout "$branch"
else
git checkout -b "$branch"
fi
fi