-
-
Save tessus/c06aecb6228e70d4385936dccb0991c0 to your computer and use it in GitHub Desktop.
Key bindings for git with fzf (https://junegunn.kr/2016/07/fzf-git/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# GIT heart FZF | |
# ------------- | |
is_in_git_repo() { | |
git rev-parse HEAD > /dev/null 2>&1 | |
} | |
# Only for interactive bash session | |
if [[ $- =~ i ]];then | |
fzf-down() { | |
fzf --height 50% "$@" --no-mouse --border --bind "shift-down:page-down,shift-up:page-up,ctrl-f:preview-page-down,ctrl-b:preview-page-up,alt-j:preview-down,alt-k:preview-up" | |
} | |
fzf_gf() { | |
is_in_git_repo || return | |
git -c color.status=always status --short | \ | |
fzf-down -m --ansi --nth 2..,.. --preview '(git diff --color=always -- {-1} | sed 1,4d; cat {-1}) | head -500' | \ | |
cut -c4- | sed 's/.* -> //' | |
} | |
# https://unix.stackexchange.com/a/174006 (sed '/^*/!H;//p;$!d;g;s/\n//') - move lines that start with * to top | |
fzf_gb() { | |
is_in_git_repo || return | |
git branch -a --color=always | grep -v '/HEAD\s' | sed '/^*/!H;//p;$!d;g;s/\n//' | \ | |
fzf-down --ansi --multi --preview-window right:70% --preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) -- | head -'$LINES | \ | |
sed 's/^..//' | cut -d' ' -f1 | sed 's#^remotes/##' | |
} | |
fzf_gt() { | |
is_in_git_repo || return | |
git tag --sort -version:refname | \ | |
fzf-down --multi --preview-window right:70% --preview 'git show --color=always {} | head -'$LINES | |
} | |
fzf_gh() { | |
is_in_git_repo || return | |
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always | \ | |
fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort' --header 'Press CTRL-S to toggle sort' --preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES | \ | |
grep -o "[a-f0-9]\{7,\}" | |
} | |
fzf_gr() { | |
is_in_git_repo || return | |
git remote -v | awk '{print $1 "\t" $2}' | uniq | \ | |
fzf-down --tac --preview 'git log --oneline --graph --date=short --pretty="format:%C(auto)%cd %h%d %s" --remotes={1} | head -200' | \ | |
cut -d$'\t' -f1 | |
} | |
fzf_gl() { | |
is_in_git_repo || return | |
git log --pretty="%C(auto)%h | %C(green)%C(bold)%ci |%C(auto)%d %s (%an)" --graph --color=always "$@" | | |
fzf --ansi --no-sort --no-mouse --layout=reverse-list --tiebreak=index --bind=ctrl-s:toggle-sort --no-border --height 100% \ | |
--bind "ctrl-f:page-down,ctrl-b:page-up,ctrl-m:execute: | |
(grep -o '[a-f0-9]\{7\}' | head -1 | | |
xargs -I % sh -c 'git show --color=always % | less -R') << 'FZF-EOF' | |
{} | |
FZF-EOF" | |
} | |
fzf_gco() { | |
is_in_git_repo || return | |
if [[ $1 != "" ]]; then | |
git checkout "$@" | |
else | |
result=`git branch -a --color=always | grep -v '/HEAD\s' | sort | | |
fzf-down --ansi --tac --preview-window right:70% --preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1) -- | head -'$LINES | | |
sed 's/^..//' | cut -d' ' -f1` | |
if [[ $result != "" ]]; then | |
if [[ $result == remotes/* ]]; then | |
git checkout --track "$(echo $result | sed 's#remotes/##')" | |
else | |
git checkout "$result" | |
fi | |
fi | |
fi | |
} | |
# git cherry pick | |
fzf_gcp() { | |
is_in_git_repo || return | |
result=`git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always $1 | \ | |
fzf-down --ansi --no-sort --reverse --multi --bind 'ctrl-s:toggle-sort,tab:toggle-up,btab:toggle-down' --header 'Press CTRL-S to toggle sort. Commits will be applied in the order they are selected.' --preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES | \ | |
grep -o "[a-f0-9]\{7,\}"` | |
if [[ $result != "" ]]; then | |
#echo git cherry-pick $result | |
git cherry-pick $result | |
fi | |
} | |
# don't really need this. fzf_gl is better | |
fzf_gd() { | |
is_in_git_repo || return | |
result=$(git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always | \ | |
fzf-down --ansi --no-sort --reverse --multi=2 --bind 'ctrl-s:toggle-sort' --header 'Commits will be swapped for the diff. Press CTRL-S to toggle sort' --preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always | head -'$LINES | \ | |
grep -o "[a-f0-9]\{7,\}") | |
case "$result" in | |
*[[:space:]]*) | |
dh=$(echo $result |awk '{printf "%s..%s", $2, $1}') | |
git diff $dh | |
;; | |
esac | |
} | |
bind '"\er": redraw-current-line' | |
bind '"\C-g\C-f": "$(fzf_gf)\e\C-e\er"' | |
bind '"\C-g\C-b": "$(fzf_gb)\e\C-e\er"' | |
bind '"\C-g\C-t": "$(fzf_gt)\e\C-e\er"' | |
bind '"\C-g\C-h": "$(fzf_gh)\e\C-e\er"' | |
bind '"\C-g\C-r": "$(fzf_gr)\e\C-e\er"' | |
bind '"\C-g\C-l": "$(fzf_gl)\e\C-e\er"' | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment