-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash_git
More file actions
208 lines (174 loc) · 4.96 KB
/
bash_git
File metadata and controls
208 lines (174 loc) · 4.96 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# Git aliases
# --------------------------------------------------------------------
alias cm="rg '>>>>>>> '"
alias ga="git add -p"
alias gaa="git add ."
alias gam="git commit --amend"
alias gau="git add $(git diff --name-only --diff-filter=U --relative)"
alias gbc="echo Total branches: `git branch | wc -l`"
alias gbu='git branch -u origin/"$(git rev-parse --abbrev-ref HEAD)" "$(git rev-parse --abbrev-ref HEAD)"'
alias gd="clear ; git diff"
alias gds="git diff --staged"
alias gdw="clear ; git diff --color-words"
alias gf="git fetch"
alias gl="git log --graph --pretty=format:'%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --date=relative"
alias gld="git log master.. --oneline"
alias glt="git log --no-walk --tags --pretty='%h %d %s' --decorate=full"
alias gm="git mergetool"
alias gmm="git merge master"
alias go="git checkout"
alias gom="git checkout master && git fetch && git pull"
alias gpl="git pull"
alias gps="git push"
alias gs="git status -sb"
alias gso="git diff --name-status origin/master"
alias gsu="git diff --name-only --diff-filter=U --relative"
alias gv="git vimdiff"
alias reset="git stash"
alias stash="git add . ; git stash"
alias throwaway="git stash"
alias vrm="vim README.md"
# Wrap git automatically by adding the following to ~/.bash_profile:
eval "$(hub alias -s)"
# Git functions
# --------------------------------------------------------------------
# fetch origin branch
fo() {
git fetch origin
git fetch origin "+$1:origin/$1"
git checkout "$1"
git pull
git branch -u origin/"$1" "$1"
}
# fshow - git commit browser
fshow() {
local out sha q
while out=$(
git log --graph --color=always \
--format="%C(auto)%h%d %s %C(black)%C(bold)%cr" "$@" |
fzf --ansi --multi --no-sort --reverse --query="$q" --print-query); do
q=$(head -1 <<< "$out")
while read sha; do
git show --color=always $sha | less -R
done < <(sed '1d;s/^[^a-z0-9]*//;/^$/d' <<< "$out" | awk '{print $1}')
done
}
# ftags - search ctags
ftags() {
local line
[ -e tags ] &&
line=$(
awk 'BEGIN { FS="\t" } !/^!/ {print toupper($4)"\t"$1"\t"$2"\t"$3}' tags |
cut -c1-80 | fzf --nth=1,2
) && ${EDITOR:-vim} $(cut -f3 <<< "$line") -c "set nocst" \
-c "silent tag $(cut -f2 <<< "$line")"
}
gb() {
git checkout $(git branch | fzf-tmux -d 15)
}
# double-check that you really want to commit to master
gc() {
clear
BRANCH_NAME=`git symbolic-ref --short HEAD`
if [ "$BRANCH_NAME" = "master" ] ; then
read -p "Are you sure you want to commit to master? [y/n] " -n 1 CONFIRM
echo ""
if [ "$CONFIRM" = "y" ] ; then
git add -p ; git commit
fi
else
git add -p ; git commit
fi
}
# remove old branches that have already been merged into master
gclean() {
DATE=`date -d '30 days ago' +%m-%d-%Y` # default date is 1 month ago
DRY_RUN=""
CURRENT_BRANCH=`git symbolic-ref --short HEAD`
BRANCHES=0
while [[ $# > 0 ]]
do
KEY="$1"
case $KEY in
# user can pass in a date
-d|--date)
DATE="$2"
;;
# show what gets deleted
--dry|--dry-run)
DRY_RUN=1
;;
esac
shift
done
for BRANCH in $(git branch --merged | grep -v $CURRENT_BRANCH) ; do
if [[ "$(git log $BRANCH --since $DATE | wc -l)" -eq 0 ]] ; then
echo "$BRANCH"
((BRANCHES++))
fi
done
if [[ -z "$DRY_RUN" ]] ; then
echo -e "\n$BRANCHES merged branches stale since $DATE"
if [[ $BRANCHES -gt 0 ]] ; then
read -p "Do you want to delete all older merged branches? [y/n] " -n 1 CONFIRM
echo ""
if [ "$CONFIRM" = "y" ] ; then
for BRANCH in $(git branch --merged | grep -v $CURRENT_BRANCH) ; do
if [[ "$(git log $BRANCH --since $DATE | wc -l)" -eq 0 ]] ; then
git branch -D $BRANCH
fi
done
fi
fi
fi
}
# git diff origin
gdo() {
BRANCH_NAME=`git symbolic-ref --short HEAD`
git diff origin/$BRANCH_NAME
}
# check out the branch matching the parameter
gob() {
local branches branch
branches=$(git branch) &&
branch=$(echo "$branches" | fzf-tmux -d 15 +m) &&
git checkout $(echo "$branch" | sed "s/.* //")
}
# pull master and merge into current branch
gomm() {
git fetch
got merge origin/master
}
# show details of last commit
gq() {
git show :/$1
}
# check for merge conflicts before continuing
grc() {
CONFLICT=`rg ">>>>>>> "`
if [ -z "$CONFLICT" ] ; then
git add . ; git rebase --continue
else
echo "$CONFLICT"
read -p "There are still merge conflicts. Do you want to merge? [y/n] " -n 1 CONFIRM
echo ""
if [ "$CONFIRM" = "y" ] ; then
git add . ; git rebase --continue
fi
fi
}
# open a PR
pr() {
BRANCH="$(git symbolic-ref HEAD)";
BRANCH="${BRANCH#refs/heads/}";
git push -u origin "$BRANCH";
URL="$(git remote get-url origin)";
HTTP_URL=${URL/:/\/}
open "https://$HTTP_URL/compare/$BRANCH?expand=1"
}
squash() {
git rebase -i HEAD~$1
}
alias sq='squash'
alias sq2='squash 2'
alias sq3='squash 3'