0

Sometimes when doing code review or coordinating interfaces with people who may not have access to my repo, I need a quick way to provide the before and after copies of a few files of interest.

This may be because they don't have access to my repo, they may not use git, and they may use other version control or "diff" type software and need to just do a quick comparison themselves between the before and after files using their own tools.

I know git diff commit~..commit will show the changes introduced by commit commit, and git log --patch shows all changes introduced by all commits, and git diff commit~..commit > patch.txt will store the git diff output into a patch file named "patch.txt", but:

Is there a git command to output before and after copies of files of interest?

Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265

1 Answers1

0

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:

  1. How to get just one file from another branch
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
  • 1
    Note that you can also use `git archive` to build a zip archive directly. (You can build either a tar or zip archive.) The default is to archive an entire commit and use the files' paths as is, but see the options: `--prefix` will insert a prefix on file names and you can list paths to archive. – torek Jan 29 '21 at 00:50
  • That looks useful. I'll have to research it more sometime. Here's one article on how to use it that I found: https://alblue.bandlem.com/2011/09/git-tip-of-week-git-archive.html. – Gabriel Staples Jan 29 '21 at 05:01