Export only modified and added files with folder structure in Git
if your commit hash is for example a9359f9, this command :
git archive -o patch.zip a9359f9 $(git diff --name-only a9359f9^..a9359f9)
will extract the files modified in the commit and place them in patch.zip while keeping the project directory structure intact.
a bit verbose, the commit hash is mentioned three times, but it seems to work for me.
got it here : http://tosbourn.com/2011/05/git/using-git-to-create-an-archive-of-changed-files/
git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT $commit_id | xargs tar -rf mytarfile.tar
Just to round this out, here is the command piped to tar. This exports the files into a tar archive.
git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT $commit_id
git diff-tree -r $commit_id
:Take a diff of the given commit to its parent(s) (including all subdirectories, not just the top directory).
--no-commit-id --name-only
:Do not output the commit SHA1. Output only the names of the affected files instead of a full diff.
--diff-filter=ACMRT
:Only show files added, copied, modified, renamed or that had their type changed (eg. file → symlink) in this commit. This leaves out deleted files.
UPDATE FROM THE COMMENT:
Base on the question context and the comments below, with the following command, you can get the ACMRT
files as a .tar
file with their folder structure.
git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT $commit_id | tar -czf file.tgz -T -
Here is a one-line command that works on Windows 7. Run it from your repository's top-level folder.
for /f "usebackq tokens=*" %A in (`git diff-tree -r --no-commit-id --name-only --diff-filter=ACMRT HEAD~1 HEAD`) do echo FA|xcopy "%~fA" "C:\git_changed_files\%A"
- echo FA answers the inevitable xcopy question about whether you're copying a file or a directory (file), and the possible question about overwriting a file (overwrite All)
- usebackq allows us to use the output from our git command as the input to our do clause
- HEAD~1 HEAD gets all differences between the previous commit and the current HEAD
- %~fA transforms the git output into fully qualified paths (needed to change forward slashes to backslashes)
- C:\git_changed_files\ is where you will find all the files that are different