git.plugin.zsh 12 KB

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