Skip to content

Instantly share code, notes, and snippets.

@tamphh
Forked from junegunn/gist:f4fca918e937e6bf5bad
Last active March 30, 2024 20:32
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tamphh/3c9a4aa07ef21232624bacb4b3f3c580 to your computer and use it in GitHub Desktop.
Save tamphh/3c9a4aa07ef21232624bacb4b3f3c580 to your computer and use it in GitHub Desktop.
Browsing git commit history with fzf
# git log show with fzf
#
# https://gist.github.com/tamphh/3c9a4aa07ef21232624bacb4b3f3c580/edit
# https://asciinema.org/a/257939
#
# git log show with fzf
gli() {
# param validation
if [[ ! `git log -n 1 $@ | head -n 1` ]] ;then
return
fi
# filter by file string
local filter
# param existed, git log for file if existed
if [ -n $@ ] && [ -f $@ ]; then
filter="-- $@"
fi
# git command
local gitlog=(
git log
--graph --color=always
--abbrev=7
--format='%C(auto)%h %an %C(blue)%s %C(yellow)%cr'
$@
)
# fzf command
local fzf=(
fzf
--ansi --no-sort --reverse --tiebreak=index
--preview "f() { set -- \$(echo -- \$@ | grep -o '[a-f0-9]\{7\}'); [ \$# -eq 0 ] || git show --color=always \$1 $filter; }; f {}"
--bind "ctrl-q:abort,ctrl-m:execute:
(grep -o '[a-f0-9]\{7\}' | head -1 |
xargs -I % sh -c 'git show --color=always % $filter | less -R') << 'FZF-EOF'
{}
FZF-EOF"
--preview-window=right:60%
)
# piping them
$gitlog | $fzf
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment