git.plugin.zsh 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 gcam='git commit -a -m'
  65. alias gcb='git checkout -b'
  66. alias gcf='git config --list'
  67. alias gcl='git clone --recursive'
  68. alias gclean='git clean -fd'
  69. alias gpristine='git reset --hard && git clean -dfx'
  70. alias gcm='git checkout master'
  71. alias gcmsg='git commit -m'
  72. alias gco='git checkout'
  73. alias gcount='git shortlog -sn'
  74. compdef gcount=git
  75. alias gcp='git cherry-pick'
  76. alias gcs='git commit -S'
  77. alias gd='git diff'
  78. alias gdca='git diff --cached'
  79. alias gdt='git diff-tree --no-commit-id --name-only -r'
  80. gdv() { git diff -w "$@" | view - }
  81. compdef _git gdv=git-diff
  82. alias gdw='git diff --word-diff'
  83. alias gf='git fetch'
  84. alias gfa='git fetch --all --prune'
  85. function gfg() { git ls-files | grep $@ }
  86. compdef gfg=grep
  87. alias gfo='git fetch origin'
  88. alias gg='git gui citool'
  89. alias gga='git gui citool --amend'
  90. ggf() {
  91. [[ "$#" != 1 ]] && local b="$(current_branch)"
  92. git push --force origin "${b:=$1}"
  93. }
  94. compdef _git ggf=git-checkout
  95. ggl() {
  96. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  97. git pull origin "${*}"
  98. else
  99. [[ "$#" == 0 ]] && local b="$(current_branch)"
  100. git pull origin "${b:=$1}"
  101. fi
  102. }
  103. compdef _git ggl=git-checkout
  104. alias ggpull='git pull origin $(current_branch)'
  105. compdef _git ggpull=git-checkout
  106. ggp() {
  107. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  108. git push origin "${*}"
  109. else
  110. [[ "$#" == 0 ]] && local b="$(current_branch)"
  111. git push origin "${b:=$1}"
  112. fi
  113. }
  114. compdef _git ggp=git-checkout
  115. alias ggpush='git push origin $(current_branch)'
  116. compdef _git ggpush=git-checkout
  117. ggpnp() {
  118. if [[ "$#" == 0 ]]; then
  119. ggl && ggp
  120. else
  121. ggl "${*}" && ggp "${*}"
  122. fi
  123. }
  124. compdef _git ggpnp=git-checkout
  125. alias ggsup='git branch --set-upstream-to=origin/$(current_branch)'
  126. ggu() {
  127. [[ "$#" != 1 ]] && local b="$(current_branch)"
  128. git pull --rebase origin "${b:=$1}"
  129. }
  130. compdef _git ggu=git-checkout
  131. alias ggpur='ggu'
  132. compdef _git ggpur=git-checkout
  133. alias gignore='git update-index --assume-unchanged'
  134. alias gignored='git ls-files -v | grep "^[[:lower:]]"'
  135. alias git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk'
  136. compdef git-svn-dcommit-push=git
  137. alias gk='\gitk --all --branches'
  138. compdef _git gk='gitk'
  139. alias gke='\gitk --all $(git log -g --pretty=format:%h)'
  140. compdef _git gke='gitk'
  141. alias gl='git pull'
  142. alias glg='git log --stat --color'
  143. alias glgp='git log --stat --color -p'
  144. alias glgg='git log --graph --color'
  145. alias glgga='git log --graph --decorate --all'
  146. alias glgm='git log --graph --max-count=10'
  147. alias glo='git log --oneline --decorate --color'
  148. alias glol="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"
  149. 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"
  150. alias glog='git log --oneline --decorate --color --graph'
  151. alias glp="_git_log_prettily"
  152. compdef _git glp=git-log
  153. alias gm='git merge'
  154. alias gmom='git merge origin/master'
  155. alias gmt='git mergetool --no-prompt'
  156. alias gmtvim='git mergetool --no-prompt --tool=vimdiff'
  157. alias gmum='git merge upstream/master'
  158. alias gp='git push'
  159. alias gpd='git push --dry-run'
  160. alias gpoat='git push origin --all && git push origin --tags'
  161. compdef _git gpoat=git-push
  162. alias gpu='git push upstream'
  163. alias gpv='git push -v'
  164. alias gr='git remote'
  165. alias gra='git remote add'
  166. alias grb='git rebase'
  167. alias grba='git rebase --abort'
  168. alias grbc='git rebase --continue'
  169. alias grbi='git rebase -i'
  170. alias grbm='git rebase master'
  171. alias grbs='git rebase --skip'
  172. alias grh='git reset HEAD'
  173. alias grhh='git reset HEAD --hard'
  174. alias grmv='git remote rename'
  175. alias grrm='git remote remove'
  176. alias grset='git remote set-url'
  177. alias grt='cd $(git rev-parse --show-toplevel || echo ".")'
  178. alias gru='git reset --'
  179. alias grup='git remote update'
  180. alias grv='git remote -v'
  181. alias gsb='git status -sb'
  182. alias gsd='git svn dcommit'
  183. alias gsi='git submodule init'
  184. alias gsps='git show --pretty=short --show-signature'
  185. alias gsr='git svn rebase'
  186. alias gss='git status -s'
  187. alias gst='git status'
  188. alias gsta='git stash'
  189. alias gstaa='git stash apply'
  190. alias gstd='git stash drop'
  191. alias gstl='git stash list'
  192. alias gstp='git stash pop'
  193. alias gsts='git stash show --text'
  194. alias gsu='git submodule update'
  195. alias gts='git tag -s'
  196. alias gunignore='git update-index --no-assume-unchanged'
  197. alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'
  198. alias gup='git pull --rebase'
  199. alias gupv='git pull --rebase -v'
  200. alias gvt='git verify-tag'
  201. alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
  202. alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit -m "--wip--"'