forked from driesvints/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction-git.zsh
More file actions
33 lines (28 loc) · 797 Bytes
/
function-git.zsh
File metadata and controls
33 lines (28 loc) · 797 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
33
git-switch() {
local branch
git rev-parse --is-inside-work-tree >/dev/null 2>&1 || {
echo "Not inside a git repository."
return 1
}
branch=$(
git for-each-ref --sort=-committerdate --format='%(refname:short)' refs/heads refs/remotes 2>/dev/null \
| grep -v 'HEAD$' \
| sed 's#^origin/##' \
| sort -u \
| fzf \
--height 50% \
--reverse \
--prompt='switch> ' \
--preview 'git log --oneline --decorate --color=always -n 15 {}' \
--preview-window=right:60%
) || return
if [[ -z "$branch" ]]; then
echo "No branches found."
return 1
fi
if git show-ref --verify --quiet "refs/heads/$branch"; then
git switch "$branch"
else
git switch -c "$branch" --track "origin/$branch"
fi
}