git.plugin.zsh 9.8 KB

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