git.zsh 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Check the installed git against a given version number
  2. #
  3. # Prints 1 if installed > input.
  4. # Prints -1 if installed < input.
  5. # Prints 0 if they are the same.
  6. function git_compare_version() {
  7. # Sanitise input. Remove dots, right-pad 0's to 3 digits.
  8. INPUT=${1//./}
  9. INPUT=$(echo $INPUT | sed -e '
  10. :again
  11. s/^.\{1,2\}$/&0/
  12. t again'
  13. )
  14. # Sanitise installed version to 3 digits as well.
  15. INSTALLED=$(git --version 2> /dev/null)
  16. INSTALLED=${INSTALLED//[git version |.]/}
  17. if [[ $INSTALLED -gt $INPUT ]]; then
  18. echo 1
  19. elif [[ $INSTALLED -lt $INPUT ]]; then
  20. echo -1
  21. else
  22. echo 0
  23. fi
  24. return 0
  25. }
  26. # Prepare compact git log (`gl`) format
  27. #
  28. # Log displays train tracks, decorations, users and dates.
  29. if [[ $(git_compare_version "1.8.3") -ge 0 ]]; then
  30. DECO_COLOUR='%C(auto)'
  31. else
  32. DECO_COLOUR='%Cgreen'
  33. fi
  34. GIT_LOG_PRETTY="'format:%C(yellow)%h %Creset%ad %Cblue%an:$DECO_COLOUR%d %Creset%s'"
  35. GIT_LOG_DEFAULTS="--graph --date=short --pretty=$GIT_LOG_PRETTY"
  36. # Useful oh-my-zsh git plugin aliases:
  37. #
  38. # ga, gc, gco, gb, gba, gm, grhh, grb/grba/grbc, gwip, gunwip
  39. #
  40. # Aliases not in the git plugin:
  41. alias ga.='git add -A .'
  42. alias gap='git add -p'
  43. alias gbav='git branch -av'
  44. alias gbd='git branch -D'
  45. alias gbv='git branch -v'
  46. alias gcob='git checkout -b'
  47. alias gcop='git checkout -p'
  48. alias gds='git diff --staged'
  49. alias gf='git fetch --all --tags && git fetch --all --prune'
  50. alias ggpush='git push -u origin $(current_branch)'
  51. alias gl="git log $GIT_LOG_DEFAULTS"
  52. alias gla='gl --all' # show all refs, not only those reachable from current branch.
  53. alias glb='gl --simplify-by-decoration' # show mostly branches and tags.
  54. alias glh="git --no-pager log --max-count=15 $GIT_LOG_DEFAULTS" # show first few.
  55. alias glp='git log --graph --decorate -p'
  56. alias gls='git log --graph --decorate --stat'
  57. alias gr='git reset'
  58. alias grh='git reset --hard'
  59. alias grs='git reset --soft'
  60. alias gs='git status'
  61. alias gsr='git show --format=raw' # all info about a commmit.
  62. # Searching history.
  63. alias glG='git log --stat -G' # Search DIFFS - changes with given text.
  64. alias glS='git log --stat -S' # Search DIFFS - changes in number of given text.
  65. alias glg='git log --grep' # Search MESSAGES.
  66. # Submodule management.
  67. alias gsm='git submodule'
  68. alias gsmpull='git submodule foreach git pull origin master'
  69. alias gsmup='git submodule sync && git submodule update --init --recursive'
  70. # Fix tracking for origin if not there.
  71. alias track='git branch --set-upstream-to origin/$(current_branch) && git fetch'
  72. # Autosquashing for simple fixups.
  73. alias grbi='git rebase -i --autosquash'
  74. alias gcf='git commit --fixup'
  75. alias gcs='git commit --squash'
  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. http://blog.ctp.com/2013/11/21/git-recovering-from-mistakes/
  81. function git_retrieve_discarded_index_changes() {
  82. for blob in $(git fsck --lost-found | awk '$2 == "blob" { print $3 }'); do
  83. git cat-file -p $blob > $blob.txt;
  84. done
  85. }