git.plugin.zsh 5.7 KB

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