How to get files between two commit which was changed or new create.

This should happen often, someone will deliver a package of the source to you, hope you complete a task after the delivery of Source, and for security, we will only deliver the transitions of the file. Usually, we will use tools or brain to write down what files has been modified, if you have to use git version control, that git can help you complete this matter.

Git Export

Git itself has the function of export, we can use the command to export Git content.

Export .tar file

git archive --output=files.tar HEAD $(git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT HEAD)

Export .zip file

git archive --format=zip --output=files.zip HEAD $(git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT HEAD)

But this command is too hard to remember, and I do not need to compress, so use Script to help work.

Windows

In the Windows need Batch file to complete,

You first need to use the Git command to get a different file list.

git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT <CommitID1> <CommitID2>

Then use the Bat command to perform the loop output.

for /f "usebackq tokens=*" %%A in (`git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT <CommitID1> <CommitID2>`) do echo FA|xcopy "%%~fA" "<ExportFolder>\%%A"

And then use the parameters into the Commite ID, and dynamically generate the output folder, this will be more efficient.

echo Eport GitDiff
set /p input1=Export Start Commit ID: 
set commitIdStart=%input1%
set /p input2=Export End Commit ID: 
if [%input2%] ==[] (set commitIdEnd=HEAD) else (set commitIdEnd=%input2%)
echo Start ID = %commitIdStart%
echo End ID = %commitIdEnd%

for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined DateTime set DateTime=%%x
echo %DateTime%
set exportFolder=%DateTime:~0,4%%DateTime:~4,2%%DateTime:~6,2%_%DateTime:~8,6%
echo %exportFolder%
for /f "usebackq tokens=*" %%A in (`git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT %commitIdStart% %commitIdEnd%`) do echo FA|xcopy "%%~fA" "GitExport\%exportFolder%\%%A"
set /p DUMMY =Hit Enter To Continue....

 

macOS

Similar to Windows, it will only use Shell Script implementation.

#!/bin/bash
echo "Export Git Diff"

read -p "Export Start Commit ID: " input1
read -p "Export End Commit ID: " input2

commitIdStart="${input1}"
if [ "${input2}" == "" ]; then
  commitIdEnd="HEAD"
else
  commitIdEnd="${input2}"
fi

echo "Start ID: $commitIdStart"
echo "End ID: $commitIdEnd"

#Create Export Folder
now=$(date +%Y%m%d_%H%M%S)
#echo "Current date: $now"
dirpath="GitExport/$now"
echo "Export Path:$dirpath"
mkdir -p $dirpath


#Get File List
files=$(git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT $commitIdStart $commitIdEnd);
#echo $files;

for item in $files ; do
    echo "Copy: $item"
  # For Linux (Todo Test)
  # cp --parents "$item" "$dirpath/$item"
  # For Mac OS
   ditto "$item" "$dirpath/$item"
done

read -p "Press enter to exit"

 

A more detailed description can refer to my github

https://github.com/lulualulu/GitDiffExport

 

Reference

https://blog.miniasp.com/post/2014/04/01/Git-Export-Only-Added-Modified-Files.aspx