'git archive inside subdirectory

I am trying to archive my project regardless of my current directory.

Project structure

main_folder/
    sub1/
    sub2/
        sub3/

If I cd to main_folder/sub2/sub3/ and run git archive it archives the contents of my current directory (sub3) only. Is there a way to get the entire contents of the HEAD and project, without having to BE in the project root?

actual git command:

git archive -v -o file.zip HEAD
git


Solution 1:[1]

Currently, the answer is no. You can use the -C <path> option and argument:

git -C "$(git rev-parse --show-toplevel)" archive -v --format zip HEAD
    #####################################

or cd yourself

(cd `git rev-parse --show-toplevel`; git archive -v --format zip HEAD) > file.zip

as a workaround.

git-archive has a second parameter to specify a path. The only reason why it can't be used to achieve what you ask for is that, in contrast to many other places in git, this path is interpreted as a relative path rather than a pathspec, which would recognize / and :(top) as a repository's root. Maybe they'll add this to git-archive someday.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hakre