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 easily create such an archive.

The first actually creates the archive;

git archive -o update.zip HEAD

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.

Finally we want to bring this all together into one nice and easy 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.


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.