better git history using fzf
Looking at past versions of a whole repository or just one file is a very common git task for me. So I build an git alias around it, which over time evolved to this script:
#!/bin/sh
file="$1"
commit=$(git log --decorate --no-merges $file | \
fzf --preview "git show --color=always --format=fuller -U0 --stat -w {1} $file" \
--border-label="$(git remote --verbose|grep push) - $(git status | head -1)" \
--layout=reverse-list --border=bottom \
--height 100% --with-nth=2.. | cut -f1 -d' ')
if test -z "$commit"; then
exit
fi
if test -n "$file"; then
git show "$commit:$file"
else
git checkout "$commit"
fiI have added it to my ~/.gitconfig like this:
[alias]
history = "!githistory"Now, if I execute git history while in a repository, I get a nice fzf selection of all commits with a preview of the commits. I can also add a filename as parameter, e.g. git history src/grid.go and only browse the commits regarding that file.
When I hit <ENTER> then the commit will be checked out or - if I am looking at one specific file - git show of that file in the selected revision will be run.