git.plugin.zsh 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #
  2. # Functions
  3. #
  4. # The name of the current branch
  5. # Back-compatibility wrapper for when this function was defined here in
  6. # the plugin, before being pulled in to core lib/git.zsh as git_current_branch()
  7. # to fix the core -> git plugin dependency.
  8. function current_branch() {
  9. git_current_branch
  10. }
  11. # Pretty log messages
  12. function _git_log_prettily(){
  13. if ! [ -z $1 ]; then
  14. git log --pretty=$1
  15. fi
  16. }
  17. compdef _git _git_log_prettily=git-log
  18. # Warn if the current branch is a WIP
  19. function work_in_progress() {
  20. if $(git log -n 1 2>/dev/null | grep -q -c "\-\-wip\-\-"); then
  21. echo "WIP!!"
  22. fi
  23. }
  24. #
  25. # Aliases
  26. # (sorted alphabetically)
  27. #
  28. alias g='git'
  29. alias ga='git add'
  30. alias gaa='git add --all'
  31. alias gapa='git add --patch'
  32. alias gau='git add --update'
  33. alias gav='git add --verbose'
  34. alias gap='git apply'
  35. alias gapt='git apply --3way'
  36. alias gb='git branch'
  37. alias gba='git branch -a'
  38. alias gbd='git branch -d'
  39. alias gbda='git branch --no-color --merged | command grep -vE "^(\+|\*|\s*(master|development|develop|devel|dev)\s*$)" | command xargs -n 1 git branch -d'
  40. alias gbD='git branch -D'
  41. alias gbl='git blame -b -w'
  42. alias gbnm='git branch --no-merged'
  43. alias gbr='git branch --remote'
  44. alias gbs='git bisect'
  45. alias gbsb='git bisect bad'
  46. alias gbsg='git bisect good'
  47. alias gbsr='git bisect reset'
  48. alias gbss='git bisect start'
  49. alias gc='git commit -v'
  50. alias gc!='git commit -v --amend'
  51. alias gcn!='git commit -v --no-edit --amend'
  52. alias gca='git commit -v -a'
  53. alias gca!='git commit -v -a --amend'
  54. alias gcan!='git commit -v -a --no-edit --amend'
  55. alias gcans!='git commit -v -a -s --no-edit --amend'
  56. alias gcam='git commit -a -m'
  57. alias gcsm='git commit -s -m'
  58. alias gcb='git checkout -b'
  59. alias gcf='git config --list'
  60. alias gcl='git clone --recurse-submodules'
  61. alias gclean='git clean -id'
  62. alias gpristine='git reset --hard && git clean -dffx'
  63. alias gcm='git checkout master'
  64. alias gcd='git checkout develop'
  65. alias gcmsg='git commit -m'
  66. alias gco='git checkout'
  67. alias gcount='git shortlog -sn'
  68. alias gcp='git cherry-pick'
  69. alias gcpa='git cherry-pick --abort'
  70. alias gcpc='git cherry-pick --continue'
  71. alias gcs='git commit -S'
  72. alias gd='git diff'
  73. alias gdca='git diff --cached'
  74. alias gdcw='git diff --cached --word-diff'
  75. alias gdct='git describe --tags $(git rev-list --tags --max-count=1)'
  76. alias gds='git diff --staged'
  77. alias gdt='git diff-tree --no-commit-id --name-only -r'
  78. alias gdw='git diff --word-diff'
  79. function gdnolock() {
  80. git diff "$@" ":(exclude)package-lock.json" ":(exclude)*.lock"
  81. }
  82. compdef _git gdnolock=git-diff
  83. function gdv() { git diff -w "$@" | view - }
  84. compdef _git gdv=git-diff
  85. alias gf='git fetch'
  86. alias gfa='git fetch --all --prune'
  87. alias gfo='git fetch origin'
  88. alias gfg='git ls-files | grep'
  89. alias gg='git gui citool'
  90. alias gga='git gui citool --amend'
  91. function ggf() {
  92. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  93. git push --force origin "${b:=$1}"
  94. }
  95. compdef _git ggf=git-checkout
  96. function ggfl() {
  97. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  98. git push --force-with-lease origin "${b:=$1}"
  99. }
  100. compdef _git ggfl=git-checkout
  101. function ggl() {
  102. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  103. git pull origin "${*}"
  104. else
  105. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  106. git pull origin "${b:=$1}"
  107. fi
  108. }
  109. compdef _git ggl=git-checkout
  110. function ggp() {
  111. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  112. git push origin "${*}"
  113. else
  114. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  115. git push origin "${b:=$1}"
  116. fi
  117. }
  118. compdef _git ggp=git-checkout
  119. function ggpnp() {
  120. if [[ "$#" == 0 ]]; then
  121. ggl && ggp
  122. else
  123. ggl "${*}" && ggp "${*}"
  124. fi
  125. }
  126. compdef _git ggpnp=git-checkout
  127. function ggu() {
  128. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  129. git pull --rebase origin "${b:=$1}"
  130. }
  131. compdef _git ggu=git-checkout
  132. alias ggpur='ggu'
  133. alias ggpull='git pull origin "$(git_current_branch)"'
  134. alias ggpush='git push origin "$(git_current_branch)"'
  135. alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
  136. alias gpsup='git push --set-upstream origin $(git_current_branch)'
  137. alias ghh='git help'
  138. alias gignore='git update-index --assume-unchanged'
  139. alias gignored='git ls-files -v | grep "^[[:lower:]]"'
  140. alias git-svn-dcommit-push='git svn dcommit && git push github master:svntrunk'
  141. alias gk='\gitk --all --branches'
  142. alias gke='\gitk --all $(git log -g --pretty=%h)'
  143. alias gl='git pull'
  144. alias glg='git log --stat'
  145. alias glgp='git log --stat -p'
  146. alias glgg='git log --graph'
  147. alias glgga='git log --graph --decorate --all'
  148. alias glgm='git log --graph --max-count=10'
  149. alias glo='git log --oneline --decorate'
  150. alias glol="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset'"
  151. alias glols="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --stat"
  152. alias glod="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"
  153. alias glods="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset' --date=short"
  154. alias glola="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --all"
  155. alias glog='git log --oneline --decorate --graph'
  156. alias gloga='git log --oneline --decorate --graph --all'
  157. alias glp="_git_log_prettily"
  158. alias gm='git merge'
  159. alias gmom='git merge origin/master'
  160. alias gmt='git mergetool --no-prompt'
  161. alias gmtvim='git mergetool --no-prompt --tool=vimdiff'
  162. alias gmum='git merge upstream/master'
  163. alias gma='git merge --abort'
  164. alias gp='git push'
  165. alias gpd='git push --dry-run'
  166. alias gpf='git push --force-with-lease'
  167. alias gpf!='git push --force'
  168. alias gpoat='git push origin --all && git push origin --tags'
  169. alias gpu='git push upstream'
  170. alias gpv='git push -v'
  171. alias gr='git remote'
  172. alias gra='git remote add'
  173. alias grb='git rebase'
  174. alias grba='git rebase --abort'
  175. alias grbc='git rebase --continue'
  176. alias grbd='git rebase develop'
  177. alias grbi='git rebase -i'
  178. alias grbm='git rebase master'
  179. alias grbs='git rebase --skip'
  180. alias grev='git revert'
  181. alias grh='git reset'
  182. alias grhh='git reset --hard'
  183. alias groh='git reset origin/$(git_current_branch) --hard'
  184. alias grm='git rm'
  185. alias grmc='git rm --cached'
  186. alias grmv='git remote rename'
  187. alias grrm='git remote remove'
  188. alias grs='git restore'
  189. alias grset='git remote set-url'
  190. alias grss='git restore --source'
  191. alias grt='cd "$(git rev-parse --show-toplevel || echo .)"'
  192. alias gru='git reset --'
  193. alias grup='git remote update'
  194. alias grv='git remote -v'
  195. alias gsb='git status -sb'
  196. alias gsd='git svn dcommit'
  197. alias gsh='git show'
  198. alias gsi='git submodule init'
  199. alias gsps='git show --pretty=short --show-signature'
  200. alias gsr='git svn rebase'
  201. alias gss='git status -s'
  202. alias gst='git status'
  203. # use the default stash push on git 2.13 and newer
  204. autoload -Uz is-at-least
  205. is-at-least 2.13 "$(git --version 2>/dev/null | awk '{print $3}')" \
  206. && alias gsta='git stash push' \
  207. || alias gsta='git stash save'
  208. alias gstaa='git stash apply'
  209. alias gstc='git stash clear'
  210. alias gstd='git stash drop'
  211. alias gstl='git stash list'
  212. alias gstp='git stash pop'
  213. alias gsts='git stash show --text'
  214. alias gstu='git stash --include-untracked'
  215. alias gstall='git stash --all'
  216. alias gsu='git submodule update'
  217. alias gsw='git switch'
  218. alias gswc='git switch -c'
  219. alias gts='git tag -s'
  220. alias gtv='git tag | sort -V'
  221. alias gtl='gtl(){ git tag --sort=-v:refname -n -l "${1}*" }; noglob gtl'
  222. alias gunignore='git update-index --no-assume-unchanged'
  223. alias gunwip='git log -n 1 | grep -q -c "\-\-wip\-\-" && git reset HEAD~1'
  224. alias gup='git pull --rebase'
  225. alias gupv='git pull --rebase -v'
  226. alias gupa='git pull --rebase --autostash'
  227. alias gupav='git pull --rebase --autostash -v'
  228. alias glum='git pull upstream master'
  229. alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
  230. alias gwip='git add -A; git rm $(git ls-files --deleted) 2> /dev/null; git commit --no-verify --no-gpg-sign -m "--wip-- [skip ci]"'
  231. alias gam='git am'
  232. alias gamc='git am --continue'
  233. alias gams='git am --skip'
  234. alias gama='git am --abort'
  235. alias gamscp='git am --show-current-patch'
  236. function grename() {
  237. if [[ -z "$1" || -z "$2" ]]; then
  238. echo "Usage: $0 old_branch new_branch"
  239. return 1
  240. fi
  241. # Rename branch locally
  242. git branch -m "$1" "$2"
  243. # Rename branch in origin remote
  244. if git push origin :"$1"; then
  245. git push --set-upstream origin "$2"
  246. fi
  247. }