Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a folder now once exported from a git repository, and the git repository itself:


I'd like to compare the files in now to the current state of the repository. While this is easily done with --git-dir=/repository.git and --work-tree=/now,

git --git-dir=/repository.git --work-tree=/now status

it is not so obvious to me, when now was a subfolder of the stuff in the git repository.

Is there a way, besides checking out the repository again and diffing the stuff to have git somehow compare the folder and the corresponding subfolder in the repository? (I know, that git is not centered around the idea of changes in single files, therefore I actually expect a well-informed no.)

share|improve this question
up vote 2 down vote accepted

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"
    if test -e "$GIT_INDEX_FILE"; then
      echo "already exists: $GIT_INDEX_FILE"
      exit 1
    git read-tree "$2" &&
    git diff
    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.

share|improve this answer
Hm, interesting idea. How do you get the temporary index file? I looked at stuff like git-read-tree and the format of the index file itself, but I haven't figured out yet apart from editing the binary file. – Boldewyn Oct 14 '10 at 10:01
You should only use Git commands to read/update a Git index file (i.e. you should not need to know about the binary, on-disk format unless you are writing your own “plumbing” command). When the GIT_INDEX_FILE environment variable is set, all Git commands will use the specified file instead of the normal index file (usually .git/index). In the case of the shell code in the answer, the git read-tree command will create the alternate index file and the git diff command will read it and compare it’s effective contents to the specified working tree. – Chris Johnsen Oct 15 '10 at 0:46

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
share|improve this answer
+1, thanks. I assume it's not possible without the mv? – Boldewyn Oct 12 '10 at 11:08
@Boldewyn: not that I know of, but I'm a git newbie. If moving is a problem, maybe you can use cp -al (copy directories hard-linking files) or bindfs. – Gilles Oct 12 '10 at 17:35
Cool, thanks for pointing me to bindfs (I really start to love the power of FUSE...). – Boldewyn Oct 14 '10 at 7:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.