Search code examples
gitdiffarchive

How to create a git diff/diff-tree/archive between 2 commits with versions of files at exactly this point (not head)


If I want to compare and export 2 commits in the past against each other, I can do a git diff to list all modified files, so far so good. But if I want to archive those files at exactly the version of the files at the last commit, how can I achieve that? Let's say a file got deleted after the last commit, git archive will fail, because the file listed by git diff could not be found anymore. Do I have to checkout the latest of the commits which I'd like to compare or is there any elegant solution?

git archive --output=test.zip HEAD $(git diff --diff-filter=ACMRTUXB --name-only SHA1 SHA2) fails for files which still existed in the diff but not in head anymore


Solution

  • You should specify the commit you want the archive to save files from explicitly:

    git archive --output=test.zip SHA2 -- $(git diff --diff-filter=ACMRTUXB --name-only SHA1 SHA2)