Skip to content

Instantly share code, notes, and snippets.

@ciases
Last active March 23, 2024 16:37
Show Gist options
  • Save ciases/ac99d8fd33f92e3c9d25 to your computer and use it in GitHub Desktop.
Save ciases/ac99d8fd33f92e3c9d25 to your computer and use it in GitHub Desktop.
Git: zip changed files + diff

GIT: zip changed files + diff

Create zip archive with changed files

git archive -o update.zip HEAD $(git diff --name-only <starting SHA> HEAD)

or

git diff --name-only sha1 sha2 > list.txt
zip archive -@ < list.txt

Git diff between two commits

git diff sha1..sha2 > update.patch

Git Alias

Add git alias to .gitconfig:

[alias]
  zip-changed-files = "!f() { git diff --name-only \"$1\" \"$2\" > list.txt; zip update -@ < list.txt; rm list.txt; }; f"

Usage:

$ git zip-changed-files <sha1> <sha2>

Note! Use bash autocomplete. Do not type insane zip-changed-files manually :3

@pinguluk
Copy link

Nice! Thanks!

@dominik246
Copy link

[alias]
zip-changed-files = "!f() { git diff --name-only \"$1\" \"$2\" > list.txt; 7z a files.7z @list.txt; rm list.txt; }; f"

7zip version.

@shihchun
Copy link

shihchun commented Feb 7, 2024

this looks like not work with multiple submodules
I wrote a bat script with sed.exe tar.exe and grep.exe in windows

@echo off
setlocal enabledelayedexpansion
set WORK_DIR=%cd%
set ORI_FOLDER=Ori
set MOD_FOLDER=Mod
set ZIP_FILE=CHGFILES.tar

mkdir %WORK_DIR%\%ORI_FOLDER%
mkdir %WORK_DIR%\%MOD_FOLDER%


for /f "delims=" %%i in ('git rev-parse --show-toplevel') do (
    set "git_root=%%i"
)
echo ====================Generate Start=====================
echo Git Root path is %git_root%
echo Wokrdir: %WORK_DIR%
echo ======================================================
cd /d "!git_root!"

echo Current PATH %cd%
set "entry="
for /f "tokens=* usebackq" %%a in (`git status --porcelain --ignore-submodules^=dirty 2^>^&1 ^| sed "s/^[ MD?]\{2\}//" ^| grep -iv "tar" ^| grep -iv "txt" ^| grep -iv "warning:" ` ) do (
  set "entry=%%a"
  set "entry=!entry:/=\!"
  echo %%a
  echo Copy Mod from code xcopy /I "!entry!" "%WORK_DIR%\%MOD_FOLDER%\!entry!" 
  @echo F | xcopy /I "!entry!" "%WORK_DIR%\%MOD_FOLDER%\!entry!" 

  md "%WORK_DIR%\%ORI_FOLDER%\!entry!\.."
  echo.
  echo Copy ORI from repo git show HEAD:%%a
  git show HEAD:%%a > "%WORK_DIR%\%ORI_FOLDER%\!entry!" 
  
  )
)
set "entry="
for /f "tokens=* usebackq" %%a in (`git submodule foreach --recursive git status 2^>^&1 --porcelain --ignore-submodules^=dirty  ^| sed 's/^[ MD?]\{2\}//p' ^| grep -iv "warning:"`) do (
  set "line=%%a"
  set "path2="
  if "!line:Entering =!" neq "!line!" (
    set "entry=!line:Entering =!"
    set "entry=!entry:~1,-1!"
    set "entry=!entry:/=\!"
  ) else (
  set "path2=!line!"
  set "path2=!path2:/=\!"
  set "path22=!path2:\=/!"
  set "path3=!entry!\!path2!"

  echo Copy Mod from code xcopy /I "!path3!" "%WORK_DIR%\%MOD_FOLDER%\!path3!" 
  @echo F | xcopy /I "!path3!" "%WORK_DIR%\%MOD_FOLDER%\!path3!" 

  cd /D %cd%\!entry!
  md "%WORK_DIR%\%ORI_FOLDER%\!path3!\.."
  echo.
  echo echo Copy ORI from submodules git show HEAD:!path22!
  echo cd /D %cd%\!entry!
  git show HEAD:!path22! > "%WORK_DIR%\%ORI_FOLDER%\!path3!"
  cd %WORK_DIR%
  )
)


cd %WORK_DIR%
git diff --submodule=diff > chg.diff
echo 'date','sha1','author','email','msg' > "git.csv"
git log --oneline -2000 --pretty=format:" '"%%cs"','"%%H"','"%%an"','"%%ae"','"%%s""' >> "git.csv"
git status head 2>&1 | grep -iv "warning:" > info_branch.txt
git status 2>&1 | grep -iv "warning:" >> info_branch.txt
git show HEAD 2>&1 | grep -iv "warning:" >> info_branch.txt

if exist %ZIP_FILE% del %ZIP_FILE%
tar -cf %ZIP_FILE% %ORI_FOLDER% %MOD_FOLDER% info_branch.txt git.csv chg.diff
xcopy /I /Y %ZIP_FILE% "%USERPROFILE%\Desktop"
del %ZIP_FILE%
del info_branch.txt
del chg.diff
del git.csv
rmdir /s /q %ORI_FOLDER%
rmdir /s /q %MOD_FOLDER%


echo =====================Generate End=====================
echo Git Root path is %git_root%
echo Wokrdir: %WORK_DIR%
echo ======================================================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment