Blog Splash

git: Archiving Files Changed Between Two Revisions

by Kerido Monday, January 18, 2010 8:48 AM

Just recently I started using git and I'm pretty excited about it. Today I needed to obtain a ZIP archive containing only files that were changed or added between a known revision and the current head (I believe, that's roughly the same as trunk in SVN). The solution I came upon is not fully automated, but it's still a HUGE time saver:

  1. Obtain a list of edited files:
    git diff --name-only HEAD __TAG_OR_REVISION__ > out.txt
    After running this command the file out.txt will contain the list of modified file names (including deleted ones), one per line.
  2. Open the out.txt file in a text editor and merge the contents into one line (i.e. replace CR/LF with a space). I also had to manually remove a few files which I didn't need for the archive.
  3. Copy this huge line to clipboard.
  4. Finally, generate the archive:
    git archive --format=zip HEAD __PASTE_HERE__ > out.zip

I'm not a command line expert and I'd love to know a better way. I'm sure it's possible on Linux, but I primarily use Windows, so it might take a cmd.exe geek to sort things out.

Comments

5/28/2010 10:58:39 AM #

Federico Poloni

The command-line tool you need to "replace the CR/LFs with spaces" is xargs (namely xargs -d '\n'). It should do the right thing also for filenames containing a space. You may be out of luck if your filenames contain a CR/LF, but If you do you are clearly looking for trouble. Smile
It's common on Linux, but with a quick googling you can find a version for Windows as well.
If you wish to manually exclude certain files from archiving, you should edit the .gitattributes file and give them the export-ignore attribute.
Enjoy!
--federico

Federico Poloni Italy

8/23/2010 8:16:54 PM #

Paul

I'm doing something similar on linux, and you could install cygwin to get a decent shell implementation. This creates a tar but you could do something along the lines of:

tar cvzf changes.tar.gz `git diff --name-status HEAD _REVISION_ | awk '/^[^D]/ {print $2}'`

The last part is to remove any files that have been deleted from the list.

Paul United Kingdom