git.plugin.zsh 8.5 KB

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