git.plugin.zsh 6.4 KB

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