rsync out of git repository or tar archive
I have a git repository and I want to rsync a particular revision of the repository into a directory. Basically I want to do this:
$ cd my-git-repo
$ git archive $MY_COMMIT > ~/blah.tar.gz
$ mkdir ~/tmp
$ cd ~/tmp
$ tar xf ~/blah.tar.gz
$ rsync -a ~/tmp/ ~/final-destination
$ cd
$ rm -r tmp
The problem with this is it requires extracting to a temporary directory and rsyncing from that. This is (theoretically) unnecessary and with a huge git repository takes a long time and requires a lot of free space. I don't just want to clear ~/final-destination and tar directly into it with:
$ git archive $MY_COMMIT | tar x -C ~/final-destination
because this also requires a lot of unnecessary work especially if ~/final-destination is a remote directory. Another option would be to checkout the particular revision and rsync 开发者_如何学Cdirectly out of the repository:
$ git checkout $MY_COMMIT
$ rsync -a --exclude .git my-git-repo/ ~/final-destination
but I'd rather not mess with the repository's working directory. I want to do all this in a script that may run in the background while I'm doing stuff in the respository.
So. Is there a way to rsync directly out of a particular revision of a repository. Or failing that can I somehow rsync from the tar archive without having to extract it?
If you don't want to rsync
out of your working tree, I'm afraid that you're stuck with extracting to a temporary directory first. (Although when I say that kind of thing on Stack Overflow, inevitably someone comes up with something more ingenious :))
One thing you could try is to stash your changes, checkout the commit you want, rsync, switch back to the previous branch then pop the stash again:
set -e
git stash
git checkout $MY_COMMIT
rsync -a --exclude .git ./ ~/final-destination
git checkout -
git stash pop
(I haven't tested that, I should warn you.)
精彩评论