git.plugin.zsh 8.8 KB

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