git archive from a specific hash from remote

2019-06-05 13:06发布

问题:

I am trying to get one directory from one remote repository but I only want to get that file from a specific hash. If I use git archive with HEAD everything is OK but when I try with a specific hash:

git archive -v --format tar --remote=ssh://....myrepo.git agithash afile > output.tgz

But I'm getting

fatal: sent error to the client: git upload-archive: archiver died with error
remote: fatal: no such ref: 9a9c309
remote: git upload-archive: archiver died with error

So I've read that I could use another upload-archive like this:

git archive -v --format tar --remote=ssh:.....myrepo.git --exec="/usr/local/bin/git upload-archive" ahash afile > output.tgz

It didn't work either. I assume is because BitBucket does not support it as per https://confluence.atlassian.com/bitbucketserverkb/git-upload-archive-archiver-died-with-error-779171761.html

So I wonder: is there another simple way to do this?

回答1:

If that commit is referenced by a branch, You could do a shallow clone directly at the right commit:

git clone <url> --depth=1 --branch <branch_name> -- <folder_name>

And then make an archive.

But you cannot directly get or clone a specific commit, for security reason.