Rip's Domain

Export files from a revision or range of revisions with Git

Posted in Git by rip747 on July 29, 2009

If there is one thing I miss about using TortoiseSVN was the convinence of exporting a group of files from a revision or range of revisions. Currently I know of no way of doing this in Git, so I decided to roll my own Git alias to accomplish this.

The alias below takes two arguments:

  1. the revision or range of revision you want to export files from
  2. the directory you want to place them in

So if you want to export all the changed files from HEAD to HEAD~2 and put them in the /cygdrive/c/temp directory, you would do:

$ git exportfiles HEAD..HEAD~2 '/cygdrive/c/temp'

The code is below:

exportfiles = !sh -c 'git diff $0 --name-only | "while read files; do mkdir -p \"$1/$(dirname $files)\"; cp -vf $files $1/$(dirname $files); done"'

Just cut and paste it into the [alias] portion of you .git/config file and you’re ready to go.

As always, if you have a better solution, please leave it in the comments below.

9 Responses

Subscribe to comments with RSS.

  1. rmclean2k said, on February 2, 2010 at 1:18 pm

    I was unable to get this to work. I got the following error after adding the alias to my config: ‘while’ is not recognized as an internal or external command, operable program or batch file.

    I’m using Git Extensions v1.81 on Windows XP SP3. The exact command I used was this: git exportfiles b5f64a..93711e ‘C:/Documents and Settings/rmclean.inc/Desktop/swc_updates’

    Any idea why this isn’t working?

  2. rip747 said, on February 2, 2010 at 4:53 pm

    the command won’t run on windows as it’s a bash script. it will only work on a unix system or on windows under cygwin.

  3. Ilya Vassilevsky said, on May 5, 2010 at 8:49 am

    Worked like a charm! Thanks so much! I slightly modified the command line for the alias to take only 1 argument (the target dir) as I needed to export only files changed in my working copy and it did it! You rock! I wouldn’t write that command myself🙂

  4. rip747 said, on May 23, 2010 at 9:45 pm

    you’re quite welcome. please, if you can, leave the modified script you created in the comment. i would love to check it out.

  5. Ilya Vassilevsky said, on May 24, 2010 at 4:01 pm

    I just got rid of the first argument:

    exportrev = !sh -c ‘git diff $0 –name-only | “while read files; do mkdir -p \”$1/$(dirname $files)\”; cp -vf $files $1/$(dirname $files); done”‘
    export = !sh -c ‘git diff –name-only | “while read files; do mkdir -p \”$0/$(dirname $files)\”; cp -vf $files $0/$(dirname $files); done”‘

  6. rip747 said, on May 24, 2010 at 4:26 pm

    excellent. thank you

  7. langjinet said, on November 8, 2010 at 10:42 am

    @Rip747, thank you, very good script.

    I’m quite new to GIT and Bash,

    here are my solution for Windows

    #——————————————————————————————
    #!/bin/bash
    exportfiles() {
    git diff $1 –name-only |
    while read files;
    do
    mkdir -p “$2/$(dirname $files)”;
    cp -vf $files “$2/$(dirname $files)”;
    #cp -f $files “$2/$(dirname $files)”;
    done
    }

    export -f exportfiles
    #——————————————————————————————

    Save above script to C:\export.rev.sh

    call that script in your console
    . /c/export.rev.sh

    #finally, you can call the function like this:
    exportfiles “head~9 head” “/d/temp”

  8. James Ehly said, on June 24, 2011 at 3:49 am

    @rip747 Thank You! I’m using msysgit on windows and this worked like a charm. This is so handy to be able to export changesets – you’ve saved me a ton of time as I was doing this by hand before.

  9. Matt said, on October 25, 2011 at 2:36 am

    If you get “No such file or directory” errors, it could just be that you removed some files since the previous commit(s)… OR…it could be due to spaces in filenames.

    Here’s a modified version that allows for spaces in filenames:

    exportfiles = !sh -c ‘git diff $0 –name-only | “while read files; do mkdir -p \”$1/$(dirname \”$files\”)\”; cp -vf \”$files\” \”$1/$(dirname \”$files\”)\”; done”‘


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: