Using Git to create an archive of changed files
A guide to creating an archive of changed files using Git
Often I have found myself in a situation where it would be really nice to have an archive of the files I have recently been working on.
Using two Git commands I can quickly create such an archive.
The first actually creates the archive:
git archive -o update.zip HEAD
This could also be written as:
git archive HEAD > update.zip
This will create an archive of the entire repository, which isn’t what we want in this instance but is pretty handy to know.
The archive function accepts a list of files to archive, which is what we want to do and is where our second command comes in.
git diff --name-only HEAD^
This will do the commit before the head, which is what I want most of the time but this could be changed to get files between two commits or anything you wanted, check out the help section in git for the diff command.
I go deeper into this in an article all about using git to show files changed in a commit.
Finally we want to bring this all together into one command, thus:
git archive -o update.zip HEAD $(git diff --name-only HEAD^)
This wraps our second command with $(), this gets ran first and passes the result as a parameter into our first command.
You can read more about git archive on the official git website.