git.plugin.zsh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 current branch name
  8. # Usage example: git pull origin $(current_branch)
  9. # Using '--quiet' with 'symbolic-ref' will not cause a fatal error (128) if
  10. # it's not a symbolic ref, but in a Git repo.
  11. function current_branch() {
  12. local ref
  13. ref=$($_omz_git_git_cmd symbolic-ref --quiet HEAD 2> /dev/null)
  14. local ret=$?
  15. if [[ $ret != 0 ]]; then
  16. [[ $ret == 128 ]] && return # no git repo.
  17. ref=$($_omz_git_git_cmd rev-parse --short HEAD 2> /dev/null) || return
  18. fi
  19. echo ${ref#refs/heads/}
  20. }
  21. # The list of remotes
  22. function current_repository() {
  23. if ! $_omz_git_git_cmd rev-parse --is-inside-work-tree &> /dev/null; then
  24. return
  25. fi
  26. echo $($_omz_git_git_cmd remote -v | cut -d':' -f 2)
  27. }
  28. # Pretty log messages
  29. function _git_log_prettily(){
  30. if ! [ -z $1 ]; then
  31. git log --pretty=$1
  32. fi
  33. }
  34. # Warn if the current branch is a WIP
  35. function work_in_progress() {
  36. if $(git log -n 1 2>/dev/null | grep -q -c "\-\-wip\-\-"); then
  37. echo "WIP!!"
  38. fi
  39. }
  40. #
  41. # Aliases
  42. # (sorted alphabetically)
  43. #
  44. alias g='git'
  45. alias ga='git add'
  46. alias gaa='git add --all'
  47. alias gapa='git add --patch'
  48. alias gb='git branch'
  49. alias gba='git branch -a'
  50. alias gbda='git branch --merged | command grep -vE "^(\*|\s*master\s*$)" | command xargs -n 1 git branch -d'
  51. alias gbl='git blame -b -w'
  52. alias gbnm='git branch --no-merged'
  53. alias gbr='git branch --remote'
  54. alias gbs='git bisect'
  55. alias gbsb='git bisect bad'
  56. alias gbsg='git bisect good'
  57. alias gbsr='git bisect reset'
  58. alias gbss='git bisect start'
  59. alias gc='git commit -v'
  60. alias gc!='git commit -v --amend'
  61. alias gca='git commit -v -a'
  62. alias gca!='git commit -v -a --amend'
  63. alias gcan!='git commit -v -a -s --no-edit --amend'
  64. alias gcb='git checkout -b'
  65. alias gcf='git config --list'
  66. alias gcl='git clone --recursive'
  67. alias gclean='git reset --hard && git clean -dfx'
  68. alias gcm='git checkout master'
  69. alias gcmsg='git commit -m'
  70. alias gco='git checkout'
  71. alias gcount='git shortlog -sn'
  72. compdef gcount=git
  73. alias gcp='git cherry-pick'
  74. alias gcs='git commit -S'
  75. alias gd='git diff'
  76. alias gdca='git diff --cached'
  77. alias gdt='git diff-tree --no-commit-id --name-only -r'
  78. gdv() { git diff -w "$@" | view - }
  79. compdef _git gdv=git-diff
  80. alias gdw='git diff --word-diff'
  81. alias gf='git fetch'
  82. alias gfa='git fetch --all --prune'
  83. function gfg() { git ls-files | grep $@ }
  84. compdef gfg=grep
  85. alias gfo='git fetch origin'
  86. alias gg='git gui citool'
  87. alias gga='git gui citool --amend'
  88. ggf() {
  89. [[ "$#" != 1 ]] && local b="$(current_branch)"
  90. git push --force origin "${b:=$1}"
  91. }
  92. compdef _git ggf=git-checkout
  93. ggl() {
  94. [[ "$#" == 0 ]] && local b="$(current_branch)"
  95. git pull origin "${b:=$1}" "${*[2,-1]}"
  96. }
  97. compdef _git ggl=git-checkout
  98. alias ggpull='ggl'
  99. compdef _git ggpull=git-checkout
  100. ggp() {
  101. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  102. git push origin "${*}"
  103. else
  104. [[ "$#" == 0 ]] && local b="$(current_branch)"
  105. git push origin "${b:=$1}"
  106. fi
  107. }
  108. compdef _git ggp=git-checkout
  109. alias ggpush='ggp'
  110. compdef _git ggpush=git-checkout
  111. ggpnp() {
  112. if [[ "$#" == 0 ]]; then
  113. ggl && ggp
  114. else
  115. ggl "${*}" && ggp "${*}"
  116. fi
  117. }
  118. compdef _git ggpnp=git-checkout
  119. alias ggsup='git branch --set-upstream-to=origin/$(current_branch)'
  120. ggu() {
  121. [[ "$#" != 1 ]] && local b="$(current_branch)"
  122. git pull --rebase origin "${b:=$1}"
  123. }
  124. compdef _git ggu=git-checkout
  125. alias ggpur='ggu'
  126. compdef _git ggpur=git-checkout
  127. alias gignore='git update-index --assume-unchanged'
  128. alias gignored='git ls-files -v | grep "^[[:lower:]]"'
  129. alias git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk'
  130. compdef git-svn-dcommit-push=git
  131. alias gk='\gitk --all --branches'
  132. compdef _git gk='gitk'
  133. alias gke='\gitk --all $(git log -g --pretty=format:%h)'
  134. compdef _git gke='gitk'
  135. alias gl='git pull'
  136. alias glg='git log --stat --color'
  137. alias glgp='git log --stat --color -p'
  138. alias glgg='git log --graph --color'
  139. alias glgga='git log --graph --decorate --all'
  140. alias glgm='git log --graph --max-count=10'
  141. alias glo='git log --oneline --decorate --color'
  142. alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  143. alias glola="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit --all"
  144. alias glog='git log --oneline --decorate --color --graph'
  145. alias glp="_git_log_prettily"
  146. compdef _git glp=git-log
  147. alias gm='git merge'
  148. alias gmom='git merge origin/master'
  149. alias gmt='git mergetool --no-prompt'
  150. alias gmtvim='git mergetool --no-prompt --tool=vimdiff'
  151. alias gmum='git merge upstream/master'
  152. alias gp='git push'
  153. alias gpd='git push --dry-run'
  154. alias gpoat='git push origin --all && git push origin --tags'
  155. compdef _git gpoat=git-push
  156. alias gpu='git push upstream'
  157. alias gpv='git push -v'
  158. alias gr='git remote'
  159. alias gra='git remote add'
  160. alias grb='git rebase'
  161. alias grba='git rebase --abort'
  162. alias grbc='git rebase --continue'
  163. alias grbi='git rebase -i'
  164. alias grbm='git rebase master'
  165. alias grbs='git rebase --skip'
  166. alias grh='git reset HEAD'
  167. alias grhh='git reset HEAD --hard'
  168. alias grmv='git remote rename'
  169. alias grrm='git remote remove'
  170. alias grset='git remote set-url'
  171. alias grt='cd $(git rev-parse --show-toplevel || echo ".")'
  172. alias gru='git reset --'
  173. alias grup='git remote update'
  174. alias grv='git remote -v'
  175. alias gsb='git status -sb'
  176. alias gsd='git svn dcommit'
  177. alias gsi='git submodule init'
  178. alias gsps='git show --pretty=short --show-signature'
  179. alias gsr='git svn rebase'
  180. alias gss='git status -s'
  181. alias gst='git status'
  182. alias gsta='git stash'
  183. alias gstaa='git stash apply'
  184. alias gstd='git stash drop'
  185. alias gstl='git stash list'
  186. alias gstp='git stash pop'
  187. alias gsts='git stash show --text'
  188. alias gsu='git submodule update'
  189. alias gts='git tag -s'
  190. alias gunignore='git update-index --no-assume-unchanged'
  191. alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'
  192. alias gup='git pull --rebase'
  193. alias gupv='git pull --rebase -v'
  194. alias gvt='git verify-tag'
  195. alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
  196. alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit -m "--wip--"'