git.plugin.zsh 8.7 KB

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