Skip to content

Instantly share code, notes, and snippets.

@wojciech-kulik
Last active October 16, 2024 22:48
Show Gist options
  • Save wojciech-kulik/17a2972deb8e0fef9bd7791b893f7db5 to your computer and use it in GitHub Desktop.
Save wojciech-kulik/17a2972deb8e0fef9bd7791b893f7db5 to your computer and use it in GitHub Desktop.
Git file history + diff (even if moved or renamed)
function file-history() {
if [ -z "$2" ]; then
file_pattern="$(basename "$1")"
else
file_pattern="$2"
fi
git log --oneline --abbrev-commit --follow "$1" |
fzf --ansi --preview "commit=\$(echo {1} | cut -d' ' -f1); \
file=\$(git show --pretty='' --name-only \$commit | grep -i '$file_pattern'); \
file=\$(echo \$file | sed 's/^$/./g'); \
git show --color=always \$commit -- \"\$file\" | diff-so-fancy"
}
# Usage:
# file-history "relative/path/to/file.ext"
# file-history "relative/path/to/file.ext" "before_rename\|after_rename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment