Skip to content

Instantly share code, notes, and snippets.

@adriancampos
Last active August 1, 2023 02:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save adriancampos/36945d04b21a2068f2facc54344b4f9c to your computer and use it in GitHub Desktop.
Save adriancampos/36945d04b21a2068f2facc54344b4f9c to your computer and use it in GitHub Desktop.
PowerShell script to export changed files between two commits

Usage:

.\createarchive.ps1

Creates an archive of the files that changed between HEAD and HEAD^

.\createarchive.ps1 e39eef

Creates an archive of the files that changed between e39eef and e39eef^

.\createarchive.ps1 e39eef 1e4bf3

Creates an archive of the files that changed between e39eef and 1e4bf3^

param([String]$firstcommit="HEAD", [String]$secondcommit="$firstcommit^")
$singlecommit = $secondcommit -eq "${firstcommit}^"
# get commit hash from name. Used for naming the zip.
$firstcommit = $(git rev-parse --short --verify $firstcommit)
$secondcommit = $(git rev-parse --short --verify $secondcommit)
$outpath = "${firstcommit}"
if (!$singlecommit) {
$outpath += "_${secondcommit}"
}
echo "Diff ${firstcommit}...${secondcommit}"
echo "Changed files:"
echo $(git diff --name-only $firstcommit $secondcommit)
echo $(git diff --name-only $firstcommit $secondcommit) >> "${outpath}.txt"
# create flattened archive
Compress-Archive -Force -DestinationPath "${outpath}_flat.zip" -Path $(git diff --name-only $firstcommit $secondcommit)
# create structured archive
git archive -o "${outpath}.zip" $firstcommit $(git diff --name-only $firstcommit $secondcommit)
echo "Created $outpath"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment