git.plugin.zsh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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'
  77. alias gcn!='git commit -v --no-edit --amend'
  78. alias gca='git commit -v -a'
  79. alias gca!='git commit -v -a --amend'
  80. alias gcan!='git commit -v -a --no-edit --amend'
  81. alias gcans!='git commit -v -a -s --no-edit --amend'
  82. alias gcam='git commit -a -m'
  83. alias gcsm='git commit -s -m'
  84. alias gcas='git commit -a -s'
  85. alias gcasm='git commit -a -s -m'
  86. alias gcb='git checkout -b'
  87. alias gcf='git config --list'
  88. function gccd() {
  89. command git clone --recurse-submodules "$@"
  90. [[ -d "$_" ]] && cd "$_" || cd "${${_:t}%.git}"
  91. }
  92. compdef _git gccd=git-clone
  93. alias gcl='git clone --recurse-submodules'
  94. alias gclean='git clean -id'
  95. alias gpristine='git reset --hard && git clean -dffx'
  96. alias gcm='git checkout $(git_main_branch)'
  97. alias gcd='git checkout $(git_develop_branch)'
  98. alias gcmsg='git commit -m'
  99. alias gco='git checkout'
  100. alias gcor='git checkout --recurse-submodules'
  101. alias gcount='git shortlog -sn'
  102. alias gcp='git cherry-pick'
  103. alias gcpa='git cherry-pick --abort'
  104. alias gcpc='git cherry-pick --continue'
  105. alias gcs='git commit -S'
  106. alias gcss='git commit -S -s'
  107. alias gcssm='git commit -S -s -m'
  108. alias gd='git diff'
  109. alias gdca='git diff --cached'
  110. alias gdcw='git diff --cached --word-diff'
  111. alias gdct='git describe --tags $(git rev-list --tags --max-count=1)'
  112. alias gds='git diff --staged'
  113. alias gdt='git diff-tree --no-commit-id --name-only -r'
  114. alias gdup='git diff @{upstream}'
  115. alias gdw='git diff --word-diff'
  116. function gdnolock() {
  117. git diff "$@" ":(exclude)package-lock.json" ":(exclude)*.lock"
  118. }
  119. compdef _git gdnolock=git-diff
  120. function gdv() { git diff -w "$@" | view - }
  121. compdef _git gdv=git-diff
  122. alias gf='git fetch'
  123. # --jobs=<n> was added in git 2.8
  124. is-at-least 2.8 "$git_version" \
  125. && alias gfa='git fetch --all --prune --jobs=10' \
  126. || alias gfa='git fetch --all --prune'
  127. alias gfo='git fetch origin'
  128. alias gfg='git ls-files | grep'
  129. alias gg='git gui citool'
  130. alias gga='git gui citool --amend'
  131. function ggf() {
  132. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  133. git push --force origin "${b:=$1}"
  134. }
  135. compdef _git ggf=git-checkout
  136. function ggfl() {
  137. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  138. git push --force-with-lease origin "${b:=$1}"
  139. }
  140. compdef _git ggfl=git-checkout
  141. function ggl() {
  142. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  143. git pull origin "${*}"
  144. else
  145. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  146. git pull origin "${b:=$1}"
  147. fi
  148. }
  149. compdef _git ggl=git-checkout
  150. function ggp() {
  151. if [[ "$#" != 0 ]] && [[ "$#" != 1 ]]; then
  152. git push origin "${*}"
  153. else
  154. [[ "$#" == 0 ]] && local b="$(git_current_branch)"
  155. git push origin "${b:=$1}"
  156. fi
  157. }
  158. compdef _git ggp=git-checkout
  159. function ggpnp() {
  160. if [[ "$#" == 0 ]]; then
  161. ggl && ggp
  162. else
  163. ggl "${*}" && ggp "${*}"
  164. fi
  165. }
  166. compdef _git ggpnp=git-checkout
  167. function ggu() {
  168. [[ "$#" != 1 ]] && local b="$(git_current_branch)"
  169. git pull --rebase origin "${b:=$1}"
  170. }
  171. compdef _git ggu=git-checkout
  172. alias ggpur='ggu'
  173. alias ggpull='git pull origin "$(git_current_branch)"'
  174. alias ggpush='git push origin "$(git_current_branch)"'
  175. alias ggsup='git branch --set-upstream-to=origin/$(git_current_branch)'
  176. alias gpsup='git push --set-upstream origin $(git_current_branch)'
  177. alias ghh='git help'
  178. alias gignore='git update-index --assume-unchanged'
  179. alias gignored='git ls-files -v | grep "^[[:lower:]]"'
  180. alias git-svn-dcommit-push='git svn dcommit && git push github $(git_main_branch):svntrunk'
  181. alias gk='\gitk --all --branches &!'
  182. alias gke='\gitk --all $(git log -g --pretty=%h) &!'
  183. alias gl='git pull'
  184. alias glg='git log --stat'
  185. alias glgp='git log --stat -p'
  186. alias glgg='git log --graph'
  187. alias glgga='git log --graph --decorate --all'
  188. alias glgm='git log --graph --max-count=10'
  189. alias glo='git log --oneline --decorate'
  190. alias glol="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset'"
  191. alias glols="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset' --stat"
  192. alias glod="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset'"
  193. alias glods="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ad) %C(bold blue)<%an>%Creset' --date=short"
  194. alias glola="git log --graph --pretty='%Cred%h%Creset -%C(auto)%d%Creset %s %Cgreen(%ar) %C(bold blue)<%an>%Creset' --all"
  195. alias glog='git log --oneline --decorate --graph'
  196. alias gloga='git log --oneline --decorate --graph --all'
  197. alias glp="_git_log_prettily"
  198. alias gm='git merge'
  199. alias gmom='git merge origin/$(git_main_branch)'
  200. alias gmtl='git mergetool --no-prompt'
  201. alias gmtlvim='git mergetool --no-prompt --tool=vimdiff'
  202. alias gmum='git merge upstream/$(git_main_branch)'
  203. alias gma='git merge --abort'
  204. alias gp='git push'
  205. alias gpd='git push --dry-run'
  206. alias gpf='git push --force-with-lease'
  207. alias gpf!='git push --force'
  208. alias gpoat='git push origin --all && git push origin --tags'
  209. alias gpr='git pull --rebase'
  210. alias gpu='git push upstream'
  211. alias gpv='git push -v'
  212. alias gr='git remote'
  213. alias gra='git remote add'
  214. alias grb='git rebase'
  215. alias grba='git rebase --abort'
  216. alias grbc='git rebase --continue'
  217. alias grbd='git rebase $(git_develop_branch)'
  218. alias grbi='git rebase -i'
  219. alias grbm='git rebase $(git_main_branch)'
  220. alias grbom='git rebase origin/$(git_main_branch)'
  221. alias grbo='git rebase --onto'
  222. alias grbs='git rebase --skip'
  223. alias grev='git revert'
  224. alias grh='git reset'
  225. alias grhh='git reset --hard'
  226. alias groh='git reset origin/$(git_current_branch) --hard'
  227. alias grm='git rm'
  228. alias grmc='git rm --cached'
  229. alias grmv='git remote rename'
  230. alias grrm='git remote remove'
  231. alias grs='git restore'
  232. alias grset='git remote set-url'
  233. alias grss='git restore --source'
  234. alias grst='git restore --staged'
  235. alias grt='cd "$(git rev-parse --show-toplevel || echo .)"'
  236. alias gru='git reset --'
  237. alias grup='git remote update'
  238. alias grv='git remote -v'
  239. alias gsb='git status -sb'
  240. alias gsd='git svn dcommit'
  241. alias gsh='git show'
  242. alias gsi='git submodule init'
  243. alias gsps='git show --pretty=short --show-signature'
  244. alias gsr='git svn rebase'
  245. alias gss='git status -s'
  246. alias gst='git status'
  247. # use the default stash push on git 2.13 and newer
  248. is-at-least 2.13 "$git_version" \
  249. && alias gsta='git stash push' \
  250. || alias gsta='git stash save'
  251. alias gstaa='git stash apply'
  252. alias gstc='git stash clear'
  253. alias gstd='git stash drop'
  254. alias gstl='git stash list'
  255. alias gstp='git stash pop'
  256. alias gsts='git stash show --text'
  257. alias gstu='gsta --include-untracked'
  258. alias gstall='git stash --all'
  259. alias gsu='git submodule update'
  260. alias gsw='git switch'
  261. alias gswc='git switch -c'
  262. alias gswm='git switch $(git_main_branch)'
  263. alias gswd='git switch $(git_develop_branch)'
  264. alias gts='git tag -s'
  265. alias gtv='git tag | sort -V'
  266. alias gtl='gtl(){ git tag --sort=-v:refname -n -l "${1}*" }; noglob gtl'
  267. alias gunignore='git update-index --no-assume-unchanged'
  268. alias gunwip='git log -n 1 | grep -q -c "\--wip--" && git reset HEAD~1'
  269. alias gup='git pull --rebase'
  270. alias gupv='git pull --rebase -v'
  271. alias gupa='git pull --rebase --autostash'
  272. alias gupav='git pull --rebase --autostash -v'
  273. alias gupom='git pull --rebase origin $(git_main_branch)'
  274. alias gupomi='git pull --rebase=interactive origin $(git_main_branch)'
  275. alias glum='git pull upstream $(git_main_branch)'
  276. alias gluc='git pull upstream $(git_current_branch)'
  277. alias gwch='git whatchanged -p --abbrev-commit --pretty=medium'
  278. 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]"'
  279. alias gwt='git worktree'
  280. alias gwta='git worktree add'
  281. alias gwtls='git worktree list'
  282. alias gwtmv='git worktree move'
  283. alias gwtrm='git worktree remove'
  284. alias gam='git am'
  285. alias gamc='git am --continue'
  286. alias gams='git am --skip'
  287. alias gama='git am --abort'
  288. alias gamscp='git am --show-current-patch'
  289. function grename() {
  290. if [[ -z "$1" || -z "$2" ]]; then
  291. echo "Usage: $0 old_branch new_branch"
  292. return 1
  293. fi
  294. # Rename branch locally
  295. git branch -m "$1" "$2"
  296. # Rename branch in origin remote
  297. if git push origin :"$1"; then
  298. git push --set-upstream origin "$2"
  299. fi
  300. }
  301. unset git_version