git.plugin.zsh 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. # Git version checking
  2. autoload -Uz is-at-least
  3. git_version="${${(As: :)$(git version 2>/dev/null)}[3]}"
  4. #
  5. # Functions
  6. #
  7. # The name of the current branch
  8. # Back-compatibility wrapper for when this function was defined here in
  9. # the plugin, before being pulled in to core lib/git.zsh as git_current_branch()
  10. # to fix the core -> git plugin dependency.
  11. function current_branch() {
  12. git_current_branch
  13. }
  14. # Pretty log messages
  15. function _git_log_prettily(){
  16. if ! [ -z $1 ]; then
  17. git log --pretty=$1
  18. fi
  19. }
  20. compdef _git _git_log_prettily=git-log
  21. # Warn if the current branch is a WIP
  22. function work_in_progress() {
  23. if $(git log -n 1 2>/dev/null | grep -q -c "\-\-wip\-\-"); then
  24. echo "WIP!!"
  25. fi
  26. }
  27. # Check if main exists and use instead of master
  28. function git_main_branch() {
  29. command git rev-parse --git-dir &>/dev/null || return
  30. local branch
  31. for branch in main trunk; do
  32. if command git show-ref -q --verify refs/heads/$branch; then
  33. echo $branch
  34. return
  35. fi
  36. done
  37. echo master
  38. }
  39. #
  40. # Aliases
  41. # (sorted alphabetically)
  42. #
  43. alias g='git'
  44. alias ga='git add'
  45. alias gaa='git add --all'
  46. alias gapa='git add --patch'
  47. alias gau='git add --update'
  48. alias gav='git add --verbose'
  49. alias gap='git apply'
  50. alias gapt='git apply --3way'
  51. alias gb='git branch'
  52. alias gba='git branch -a'
  53. alias gbd='git branch -d'
  54. alias gbda='git branch --no-color --merged | command grep -vE "^(\+|\*|\s*($(git_main_branch)|development|develop|devel|dev)\s*$)" | command xargs -n 1 git branch -d'
  55. alias gbD='git branch -D'
  56. alias gbl='git blame -b -w'
  57. alias gbnm='git branch --no-merged'
  58. alias gbr='git branch --remote'
  59. alias gbs='git bisect'
  60. alias gbsb='git bisect bad'
  61. alias gbsg='git bisect good'
  62. alias gbsr='git bisect reset'
  63. alias gbss='git bisect start'
  64. alias gc='git commit -v'
  65. alias gc!='git commit -v --amend'
  66. alias gcn!='git commit -v --no-edit --amend'
  67. alias gca='git commit -v -a'
  68. alias gca!='git commit -v -a --amend'
  69. alias gcan!='git commit -v -a --no-edit --amend'
  70. alias gcans!='git commit -v -a -s --no-edit --amend'
  71. alias gcam='git commit -a -m'
  72. alias gcsm='git commit -s -m'
  73. alias gcas='git commit -a -s'
  74. alias gcasm='git commit -a -s -m'
  75. alias gcb='git checkout -b'
  76. alias gcf='git config --list'
  77. alias gcl='git clone --recurse-submodules'
  78. alias gclean='git clean -id'
  79. alias gpristine='git reset --hard && git clean -dffx'
  80. alias gcm='git checkout $(git_main_branch)'
  81. alias gcd='git checkout develop'
  82. alias gcmsg='git commit -m'
  83. alias gco='git checkout'
  84. alias gcount='git shortlog -sn'
  85. alias gcp='git cherry-pick'
  86. alias gcpa='git cherry-pick --abort'
  87. alias gcpc='git cherry-pick --continue'
  88. alias gcs='git commit -S'
  89. alias gd='git diff'
  90. alias gdca='git diff --cached'
  91. alias gdcw='git diff --cached --word-diff'
  92. alias gdct='git describe --tags $(git rev-list --tags --max-count=1)'
  93. alias gds='git diff --staged'
  94. alias gdt='git diff-tree --no-commit-id --name-only -r'
  95. alias gdw='git diff --word-diff'
  96. function gdnolock() {
  97. git diff "$@" ":(exclude)package-lock.json" ":(exclude)*.lock"
  98. }
  99. compdef _git gdnolock=git-diff
  100. function gdv() { git diff -w "$@" | view - }
  101. compdef _git gdv=git-diff
  102. alias gf='git fetch'
  103. # --jobs=<n> was added in git 2.8
  104. is-at-least 2.8 "$git_version" \
  105. && alias gfa='git fetch --all --prune --jobs=10' \
  106. || alias gfa='git fetch --all --prune'
  107. alias gfo='git fetch origin'
  108. alias gfg='git ls-files | grep'
  109. alias gg='git gui citool'
  110. alias gga='git gui citool --amend'
  111. function ggf() {
  112. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  113. git push --force origin "${b:=$1}"
  114. }
  115. compdef _git ggf=git-checkout
  116. function ggfl() {
  117. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  118. git push --force-with-lease origin "${b:=$1}"
  119. }
  120. compdef _git ggfl=git-checkout
  121. function ggl() {
  122. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  123. git pull origin "${*}"
  124. else
  125. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  126. git pull origin "${b:=$1}"
  127. fi
  128. }
  129. compdef _git ggl=git-checkout
  130. function ggp() {
  131. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  132. git push origin "${*}"
  133. else
  134. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  135. git push origin "${b:=$1}"
  136. fi
  137. }
  138. compdef _git ggp=git-checkout
  139. function ggpnp() {
  140. if [[ "$#" == 0 ]]; then
  141. ggl && ggp
  142. else
  143. ggl "${*}" && ggp "${*}"
  144. fi
  145. }
  146. compdef _git ggpnp=git-checkout
  147. function ggu() {
  148. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  149. git pull --rebase origin "${b:=$1}"
  150. }
  151. compdef _git ggu=git-checkout
  152. alias ggpur='ggu'
  153. alias ggpull='git pull origin "$(git_current_branch)"'
  154. alias ggpush='git push origin "$(git_current_branch)"'
  155. alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
  156. alias gpsup='git push --set-upstream origin $(git_current_branch)'
  157. alias ghh='git help'
  158. alias gignore='git update-index --assume-unchanged'
  159. alias gignored='git ls-files -v | grep "^[[:lower:]]"'
  160. alias git-svn-dcommit-push='git svn dcommit && git push github $(git_main_branch):svntrunk'
  161. alias gk='\gitk --all --branches'
  162. alias gke='\gitk --all $(git log -g --pretty=%h)'
  163. alias gl='git pull'
  164. alias glg='git log --stat'
  165. alias glgp='git log --stat -p'
  166. alias glgg='git log --graph'
  167. alias glgga='git log --graph --decorate --all'
  168. alias glgm='git log --graph --max-count=10'
  169. alias glo='git log --oneline --decorate'
  170. alias glol="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
  171. alias glols="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --stat"
  172. alias glod="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"
  173. alias glods="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset' --date=short"
  174. alias glola="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --all"
  175. alias glog='git log --oneline --decorate --graph'
  176. alias gloga='git log --oneline --decorate --graph --all'
  177. alias glp="_git_log_prettily"
  178. alias gm='git merge'
  179. alias gmom='git merge origin/$(git_main_branch)'
  180. alias gmt='git mergetool --no-prompt'
  181. alias gmtvim='git mergetool --no-prompt --tool=vimdiff'
  182. alias gmum='git merge upstream/$(git_main_branch)'
  183. alias gma='git merge --abort'
  184. alias gp='git push'
  185. alias gpd='git push --dry-run'
  186. alias gpf='git push --force-with-lease'
  187. alias gpf!='git push --force'
  188. alias gpoat='git push origin --all && git push origin --tags'
  189. alias gpu='git push upstream'
  190. alias gpv='git push -v'
  191. alias gr='git remote'
  192. alias gra='git remote add'
  193. alias grb='git rebase'
  194. alias grba='git rebase --abort'
  195. alias grbc='git rebase --continue'
  196. alias grbd='git rebase develop'
  197. alias grbi='git rebase -i'
  198. alias grbm='git rebase $(git_main_branch)'
  199. alias grbo='git rebase --onto'
  200. alias grbs='git rebase --skip'
  201. alias grev='git revert'
  202. alias grh='git reset'
  203. alias grhh='git reset --hard'
  204. alias groh='git reset origin/$(git_current_branch) --hard'
  205. alias grm='git rm'
  206. alias grmc='git rm --cached'
  207. alias grmv='git remote rename'
  208. alias grrm='git remote remove'
  209. alias grs='git restore'
  210. alias grset='git remote set-url'
  211. alias grss='git restore --source'
  212. alias grst='git restore --staged'
  213. alias grt='cd "$(git rev-parse --show-toplevel || echo .)"'
  214. alias gru='git reset --'
  215. alias grup='git remote update'
  216. alias grv='git remote -v'
  217. alias gsb='git status -sb'
  218. alias gsd='git svn dcommit'
  219. alias gsh='git show'
  220. alias gsi='git submodule init'
  221. alias gsps='git show --pretty=short --show-signature'
  222. alias gsr='git svn rebase'
  223. alias gss='git status -s'
  224. alias gst='git status'
  225. # use the default stash push on git 2.13 and newer
  226. is-at-least 2.13 "$git_version" \
  227. && alias gsta='git stash push' \
  228. || alias gsta='git stash save'
  229. alias gstaa='git stash apply'
  230. alias gstc='git stash clear'
  231. alias gstd='git stash drop'
  232. alias gstl='git stash list'
  233. alias gstp='git stash pop'
  234. alias gsts='git stash show --text'
  235. alias gstu='gsta --include-untracked'
  236. alias gstall='git stash --all'
  237. alias gsu='git submodule update'
  238. alias gsw='git switch'
  239. alias gswc='git switch -c'
  240. alias gts='git tag -s'
  241. alias gtv='git tag | sort -V'
  242. alias gtl='gtl(){ git tag --sort=-v:refname -n -l "${1}*" }; noglob gtl'
  243. alias gunignore='git update-index --no-assume-unchanged'
  244. alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'
  245. alias gup='git pull --rebase'
  246. alias gupv='git pull --rebase -v'
  247. alias gupa='git pull --rebase --autostash'
  248. alias gupav='git pull --rebase --autostash -v'
  249. alias glum='git pull upstream $(git_main_branch)'
  250. alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
  251. alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]"'
  252. alias gam='git am'
  253. alias gamc='git am --continue'
  254. alias gams='git am --skip'
  255. alias gama='git am --abort'
  256. alias gamscp='git am --show-current-patch'
  257. function grename() {
  258. if [[ -z "$1" || -z "$2" ]]; then
  259. echo "Usage: $0 old_branch new_branch"
  260. return 1
  261. fi
  262. # Rename branch locally
  263. git branch -m "$1" "$2"
  264. # Rename branch in origin remote
  265. if git push origin :"$1"; then
  266. git push --set-upstream origin "$2"
  267. fi
  268. }
  269. unset git_version