Julius Plenz – Blog

git: find absolute path to tracked file

If you need to name a blob by name, you can't trust Git to honor the current working directory. If you are in a subdir, git show HEAD^:file won't work unless you prepend the subdirectory name.

But there's a sort of basename for absolute paths within a Git repo:

git ls-tree --name-only --full-name HEAD file

This honors $PWD and thus returns a pathname that's unique across the repository. You can build a simple "diff to previous version" script with this now:

#!/bin/sh

test ! -z "$1" || exit 1
temp=${1%.*}.HEAD^1.$$.${1#*.}
fullpath=`git ls-tree --name-only --full-name HEAD^ $1`

test ! -z "$1" || exit 2

echo "extracting '$fullpath' from HEAD^..."
git show HEAD^:$fullpath > $temp
vim -fd $1 $temp
echo "cleaning up..."
rm $temp

This can act as a simplified mergetool when you just want to review changes made to some file.

posted 2011-03-08 tagged git