Export files from a revision or range of revisions with Git
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:
- the revision or range of revision you want to export files from
- 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.
Advertisement
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?
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.
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
you’re quite welcome. please, if you can, leave the modified script you created in the comment. i would love to check it out.
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”‘
excellent. thank you
@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”
@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.
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”‘