git.zsh 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # preferred git shortcuts, not in the git plugin
  2. #compare the provided version of git to the version installed and on path
  3. #prints 1 if input version <= installed version
  4. #prints -1 otherwise
  5. function git_compare_version() {
  6. local INPUT_GIT_VERSION=$1;
  7. local INSTALLED_GIT_VERSION
  8. INPUT_GIT_VERSION=(${(s/./)INPUT_GIT_VERSION});
  9. INSTALLED_GIT_VERSION=($(command git --version 2>/dev/null));
  10. INSTALLED_GIT_VERSION=(${(s/./)INSTALLED_GIT_VERSION[3]});
  11. for i in {1..3}; do
  12. if [[ $INSTALLED_GIT_VERSION[$i] -gt $INPUT_GIT_VERSION[$i] ]]; then
  13. echo 1
  14. return 0
  15. fi
  16. if [[ $INSTALLED_GIT_VERSION[$i] -lt $INPUT_GIT_VERSION[$i] ]]; then
  17. echo -1
  18. return 0
  19. fi
  20. done
  21. echo 1
  22. return 0
  23. }
  24. POST_1_8_3_GIT=$(git_compare_version "1.8.3")
  25. unset -f git_compare_version
  26. # As of git 1.8.3 decorations can be coloured automatically according to what
  27. # they are i.e. tags, branches, etc.
  28. if [[ $POST_1_8_3_GIT -gt 0 ]]; then
  29. DECO_COLOUR='%C(auto)'
  30. else
  31. DECO_COLOUR='%Cgreen'
  32. fi
  33. GIT_LOG_PRETTY="'format:%C(yellow)%h %Creset%ad %Cblue%an:$DECO_COLOUR%d %Creset%s'"
  34. # Log displays train tracks, decorations, users and dates
  35. GIT_LOG_DEFAULTS="--graph --date=short --pretty=$GIT_LOG_PRETTY"
  36. alias ga.='git add -A .'
  37. alias gap='git add -p'
  38. alias gbav='git branch -av'
  39. alias gbd='git branch -D'
  40. alias gbv='git branch -v'
  41. alias gcob='git checkout -b'
  42. alias gds='git diff --staged'
  43. alias gf='git fetch --all --tags && git fetch --all --prune'
  44. alias ggpush='git push -u origin $(current_branch)'
  45. alias gl="git log $GIT_LOG_DEFAULTS"
  46. alias gla='gl --all' # show all refs, not just current branch history.
  47. alias glb='gl --simplify-by-decoration' # concise branch and tag log.
  48. alias glh="git --no-pager log --max-count=15 $GIT_LOG_DEFAULTS" # show first few.
  49. alias glp='git log --graph --decorate -p'
  50. alias gls='git log --graph --decorate --stat'
  51. alias gr='git reset'
  52. alias grh='git reset --hard'
  53. alias grs='git reset --soft'
  54. alias gs='git status'
  55. alias gsr='git show --format=raw' # all info about a commmit.
  56. compdef ggpush=git
  57. # searching history.
  58. alias glG='git log --stat -G' # Search DIFFS - changes with given text.
  59. alias glS='git log --stat -S' # Search DIFFS - changes in number of given text.
  60. alias glg='git log --grep' # search commit MESSAGES.
  61. # git submodule management.
  62. alias gsm='git submodule'
  63. alias gsmpull='git submodule foreach git pull origin master'
  64. alias gsmup='git submodule sync && git submodule update --init --recursive'
  65. # fix tracking for origin if not there.
  66. alias track='git branch --set-upstream-to origin/$(current_branch) && git fetch'
  67. # autosquashing for simple fixups.
  68. alias grbi='git rebase -i --autosquash'
  69. alias gcf='git commit --fixup'
  70. alias gcs='git commit --squash'
  71. # useful omz git plugin ones include:
  72. # ga, gc, gco, gb, gba, gm, grhh, grb, gwip, gunwip
  73. #
  74. # ggpush has been removed from oh-my-zsh!
  75. # ggpush translates into `git push origin <current branch>`.
  76. # Recover indexed/staged changes that were lost.
  77. #
  78. # By a careless reset, for example. This function locates all dangling/orphaned
  79. # blobs and puts them in text files. These files can then be checked for the
  80. # lost changes.
  81. #
  82. # Most tidy method found so far:
  83. # http://blog.ctp.com/2013/11/21/git-recovering-from-mistakes/
  84. function git_retrieve_discarded_index_changes() {
  85. for blob in $(git fsck --lost-found | awk '$2 == "blob" { print $3 }'); do
  86. git cat-file -p $blob > $blob.txt;
  87. done
  88. }