git.plugin.zsh 12 KB

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