git.plugin.zsh 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  95. git pull origin "${*}"
  96. else
  97. [[ "$#" == 0 ]] && local b="$(current_branch)"
  98. git pull origin "${b:=$1}"
  99. fi
  100. }
  101. compdef _git ggl=git-checkout
  102. alias ggpull='git pull origin $(current_branch)'
  103. compdef _git ggpull=git-checkout
  104. ggp() {
  105. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  106. git push origin "${*}"
  107. else
  108. [[ "$#" == 0 ]] && local b="$(current_branch)"
  109. git push origin "${b:=$1}"
  110. fi
  111. }
  112. compdef _git ggp=git-checkout
  113. alias ggpush='git push origin $(current_branch)'
  114. compdef _git ggpush=git-checkout
  115. ggpnp() {
  116. if [[ "$#" == 0 ]]; then
  117. ggl && ggp
  118. else
  119. ggl "${*}" && ggp "${*}"
  120. fi
  121. }
  122. compdef _git ggpnp=git-checkout
  123. alias ggsup='git branch --set-upstream-to=origin/$(current_branch)'
  124. ggu() {
  125. [[ "$#" != 1 ]] && local b="$(current_branch)"
  126. git pull --rebase origin "${b:=$1}"
  127. }
  128. compdef _git ggu=git-checkout
  129. alias ggpur='ggu'
  130. compdef _git ggpur=git-checkout
  131. alias gignore='git update-index --assume-unchanged'
  132. alias gignored='git ls-files -v | grep "^[[:lower:]]"'
  133. alias git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk'
  134. compdef git-svn-dcommit-push=git
  135. alias gk='\gitk --all --branches'
  136. compdef _git gk='gitk'
  137. alias gke='\gitk --all $(git log -g --pretty=format:%h)'
  138. compdef _git gke='gitk'
  139. alias gl='git pull'
  140. alias glg='git log --stat --color'
  141. alias glgp='git log --stat --color -p'
  142. alias glgg='git log --graph --color'
  143. alias glgga='git log --graph --decorate --all'
  144. alias glgm='git log --graph --max-count=10'
  145. alias glo='git log --oneline --decorate --color'
  146. alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  147. 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"
  148. alias glog='git log --oneline --decorate --color --graph'
  149. alias glp="_git_log_prettily"
  150. compdef _git glp=git-log
  151. alias gm='git merge'
  152. alias gmom='git merge origin/master'
  153. alias gmt='git mergetool --no-prompt'
  154. alias gmtvim='git mergetool --no-prompt --tool=vimdiff'
  155. alias gmum='git merge upstream/master'
  156. alias gp='git push'
  157. alias gpd='git push --dry-run'
  158. alias gpoat='git push origin --all && git push origin --tags'
  159. compdef _git gpoat=git-push
  160. alias gpu='git push upstream'
  161. alias gpv='git push -v'
  162. alias gr='git remote'
  163. alias gra='git remote add'
  164. alias grb='git rebase'
  165. alias grba='git rebase --abort'
  166. alias grbc='git rebase --continue'
  167. alias grbi='git rebase -i'
  168. alias grbm='git rebase master'
  169. alias grbs='git rebase --skip'
  170. alias grh='git reset HEAD'
  171. alias grhh='git reset HEAD --hard'
  172. alias grmv='git remote rename'
  173. alias grrm='git remote remove'
  174. alias grset='git remote set-url'
  175. alias grt='cd $(git rev-parse --show-toplevel || echo ".")'
  176. alias gru='git reset --'
  177. alias grup='git remote update'
  178. alias grv='git remote -v'
  179. alias gsb='git status -sb'
  180. alias gsd='git svn dcommit'
  181. alias gsi='git submodule init'
  182. alias gsps='git show --pretty=short --show-signature'
  183. alias gsr='git svn rebase'
  184. alias gss='git status -s'
  185. alias gst='git status'
  186. alias gsta='git stash'
  187. alias gstaa='git stash apply'
  188. alias gstd='git stash drop'
  189. alias gstl='git stash list'
  190. alias gstp='git stash pop'
  191. alias gsts='git stash show --text'
  192. alias gsu='git submodule update'
  193. alias gts='git tag -s'
  194. alias gunignore='git update-index --no-assume-unchanged'
  195. alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'
  196. alias gup='git pull --rebase'
  197. alias gupv='git pull --rebase -v'
  198. alias gvt='git verify-tag'
  199. alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
  200. alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit -m "--wip--"'