git.zsh 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Git version string
  2. GIT_VERSION=$(git --version 2> /dev/null)
  3. GIT_VERSION=${GIT_VERSION:12}
  4. # Prepare compact git log (`gl`) format
  5. #
  6. # Log displays train tracks, decorations, users and dates.
  7. if [[ $(version-compare $GIT_VERSION "1.8.3") -ge 0 ]]; then
  8. DECO_COLOUR='%C(auto)'
  9. else
  10. DECO_COLOUR='%Cgreen'
  11. fi
  12. GIT_LOG_PRETTY="'format:%C(yellow)%h %Creset%ad %Cblue%an:$DECO_COLOUR%d %Creset%s'"
  13. GIT_LOG_DEFAULTS="--graph --date=short --pretty=$GIT_LOG_PRETTY"
  14. # Useful oh-my-zsh git plugin aliases:
  15. #
  16. # ga, gc, gco, gb, gba, gm, grhh, grb/grba/grbc, gwip, gunwip
  17. #
  18. # Aliases not in the git plugin:
  19. alias ga.='git add -A .'
  20. alias gap='git add -p'
  21. alias gbav='git branch -av'
  22. alias gbd='git branch -D'
  23. alias gbv='git branch -v'
  24. alias gcob='git checkout -b'
  25. alias gcop='git checkout -p'
  26. alias gds='git diff --staged'
  27. alias gf='git fetch --all --tags && git fetch --all --prune'
  28. alias ggpush='git push -u origin $(current_branch)'
  29. alias gl="git log $GIT_LOG_DEFAULTS"
  30. alias gla='gl --all' # show all refs, not only those reachable from current branch.
  31. alias glb='gl --simplify-by-decoration' # show mostly branches and tags.
  32. alias glh="git --no-pager log --max-count=15 $GIT_LOG_DEFAULTS" # show first few.
  33. alias glp='git log --graph --decorate -p'
  34. alias gls='git log --graph --decorate --stat'
  35. alias gr='git reset'
  36. alias grh='git reset --hard'
  37. alias grs='git reset --soft'
  38. alias gs='git status'
  39. alias gsr='git show --format=raw' # all info about a commmit.
  40. # Searching history.
  41. alias glG='git log --stat -G' # Search DIFFS - changes with given text.
  42. alias glS='git log --stat -S' # Search DIFFS - changes in number of given text.
  43. alias glg='git log --grep' # Search MESSAGES.
  44. # Submodule management.
  45. alias gsm='git submodule'
  46. alias gsmpull='git submodule foreach git pull origin master'
  47. alias gsmup='git submodule sync && git submodule update --init --recursive'
  48. # Fix tracking for origin if not there.
  49. alias track='git branch --set-upstream-to origin/$(current_branch) && git fetch'
  50. # Autosquashing for simple fixups.
  51. alias grbi='git rebase -i --autosquash'
  52. alias gcf='git commit --fixup'
  53. alias gcs='git commit --squash'
  54. # Recover indexed/staged changes that were lost.
  55. #
  56. # By a careless reset, for example. This function locates all dangling/orphaned
  57. # blobs and puts them in text files. These files can then be checked for the
  58. # lost changes. http://blog.ctp.com/2013/11/21/git-recovering-from-mistakes/
  59. function git_retrieve_discarded_index_changes() {
  60. for blob in $(git fsck --lost-found | awk '$2 == "blob" { print $3 }'); do
  61. git cat-file -p $blob > $blob.txt;
  62. done
  63. }