git.plugin.zsh 8.7 KB

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