git.plugin.zsh 7.9 KB

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