Comparing a folder with a git repository's subfolder
You can recreate the directory structure leading to the subfolder and take advantage of the --relative
option to commands such as diff
and log
(but not status
). For example, if you have a checkout of sub/dir
:
mkdir -p sub
mv now sub/dir
git --git-dir=/repository.git diff --relative=sub/dir
You can populate a temporary index file with just the contents of the subdirectory of the repository and then diff the existing files against it.
diff-sub() { : usage: diff-sub repo treeish-in-repo external-dir
(
GIT_DIR="$1" GIT_WORK_TREE="$3"
GIT_INDEX_FILE=/tmp/.git-index--now-sub.tmp
if test -e "$GIT_INDEX_FILE"; then
echo "already exists: $GIT_INDEX_FILE"
exit 1
fi
export GIT_DIR GIT_INDEX_FILE GIT_WORK_TREE
git read-tree "$2" &&
git diff
ec=$?
rm -f "$GIT_INDEX_FILE"
exit "$ec"
)
}
: diff sub from master in /repository.git against /now
diff-sub /repository.git master:sub /now
Note: If you run other Git commands that compare against the normal, full tree (e.g. git status
or git diff --cached
instead of git diff
), then you will see odd-looking results since both the index and the working tree only contain a portion of the normal, full tree.