git.plugin.zsh 9.0 KB

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