I couldn't find a single git
command that does this. So, I used the following sequence of commands. Here's what I came up with:
Assuming your goal is to have both files ("before" and "after") in your possession when done--ex: to send to a colleague or co-worker, do the following.
# Create a directory named "new" and copy all files of interest into it. This assumes
# you are already on your more-recent, or "new" commit.
mkdir new
cp path/to/file1.c new
cp path/to/file2.c new
cp path/to/file3.c new
# Check out the "old" version of the files from the previous_commit_hash,
# make an "old" dir for them, and copy them into it.
git checkout previous_commit_hash -- path/to/file1.c path/to/file2.c path/to/file3.c
mkdir old
cp path/to/file1.c old
cp path/to/file2.c old
cp path/to/file3.c old
# Checking out the above files automatically stages them for commit, for some reason,
# so unstage them with `git reset`
git reset
# Check out the new files again from HEAD so you are back where you started.
# This causes the newer versions of these files to again be checked out and to
# overwrite the older versions of these files you had just checked-out above.
git checkout -- path/to/file1.c path/to/file2.c path/to/file3.c
# Package up the "new" and "old" directories into a "files.zip" zip file to send
# to a friend to look at on their end with their favorite difftool
# or merge tool, such as meld or whatever!
zip -r files.zip old new
# When done, run `git status` to see that you have 2 new directories plus the new
# .zip file.
git status
The output of git status
from above will show you have these untracked files now, which is expected:
Untracked files:
(use "git add <file>..." to include in what will be committed)
files.zip
new/
old/
When done with them, remove them with:
rm -r files.zip old new
...and git status
should now be clean.
This could/should be easily scripted. If anyone wants to write a bash
shell script for it, go for it. Otherwise, I'm sure I'll do it the next time I need to perform this operation.
References:
- How to get just one file from another branch