-
Notifications
You must be signed in to change notification settings - Fork 1
/
git.zsh
127 lines (103 loc) · 2.62 KB
/
git.zsh
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Open main in browser
alias web='gh repo view --web'
# Open current branch in browser
alias webbranch='gh repo view --web --branch $(git symbolic-ref --quiet --short HEAD )'
# Open PR for current branch in browser
alias open-pr='gh pr create --web'
# Browse pulls for current repo
alias pulls='gh pr list --web'
# git aliases
alias gco='git checkout'
alias gpo='git push origin'
alias gpo1='git push --set-upstream origin $(git symbolic-ref --quiet --short HEAD )'
alias gst='git status'
alias gcam='git add . && git commit -am'
alias gcam!='git add . && git commit --no-verify -am'
alias gkm='git commit -m'
alias gkm!='git commit --no-verify -m'
# Thanks, [Elijah](https://twitter.com/elijahmanor/status/1562077209321512965)!
alias branchy="branches 20 | fzf --header \"Switch to recent branch\" --pointer=\"\" | xargs git switch"
alias unstashy="stashes 100 | fzf --header \"Apply recent stash\" --pointer=\"\" | cut -d: -f1 | xargs git stash apply"
function branch() {
git checkout -b $1
}
function stash() {
git add .
if [[ $1 ]] then
git stash push -m "$1"
else
git stash push
fi
}
function unstash() {
re='^[0-9]+$'
if [[ $1 ]] then
if [[ $1 =~ $re ]] then
echo "Applying stash@{$1}..."
git stash apply stash@{$1}
else
echo "Applying stash named "$1"..."
git stash apply $(git stash list | grep "$1" | cut -d: -f1)
fi
else
echo "Applying most recent stash..."
git stash apply
fi
}
function sync() {
local mainline=$(main_or_master)
git checkout $mainline
if [[ `git remote -v | grep upstream` ]]; then
echo "syncing to upstream..."
git pull upstream $mainline
git push origin
else
echo "syncing to origin..."
git pull origin $mainline
fi
}
function main_or_master() {
if (branch_exists main); then
echo 'main'
else
echo 'master'
fi
}
function branches() {
COUNT=${1:-5}
git branch --sort=-committerdate | head -n $COUNT
}
function stashes() {
COUNT=${1:-5}
git stash list | head -n $COUNT
}
function branch_exists() {
local branch=${1}
local exists=$(git branch --list ${branch})
if [[ -z ${exists} ]]; then
return 1
else
return 0
fi
}
function rebaseonmain() {
local mainline=$(main_or_master)
if [[ `git status --porcelain` ]]; then
local needToStashAndUnstash=true
else
local needToStashAndUnstash=false
fi
if [[ "$needToStashAndUnstash" = true ]]
then
stash
fi
echo "syncing $mainline branch to upstream...."
sync
git checkout -
echo "rebasing on $mainline...."
git rebase $mainline
if [[ "$needToStashAndUnstash" = true ]]
then
unstash
fi
}