31

I'm using git-archive to archive a subdirectory in a git repo, like so:

git archive -o ../subarchive.zip HEAD subdir/*

However the resulting archive maintains the subdir/ directory structure, i.e. the contents are:

subdir/
   whatever.js
   morestuff.js

When I actually want whatever.js and morestuff.js at the root of the archive.

How do? Thanks.

wildabeast
  • 1,635
  • 3
  • 15
  • 31

1 Answers1

50

You can do that like this:

git archive -o ../subarchive.zip HEAD:subdir

By the way, an easy way to play with the command and see what it will generate is if you use it in this form:

git archive --format=tar HEAD:subdir | tar t
git archive --format=tar HEAD subdir | tar t
# ... and so on ...

Once you see what you're looking for, you can change the format and use the -o flag to actually create the archive.

janos
  • 115,756
  • 24
  • 210
  • 226
  • 2
    Is there any way to do this efficiently for remote repos, e.g. to fetch just one dir/one revision of a gigantic repo that's not practical to clone? I tried `--remote` but it was ridiculously slow. – R.. GitHub STOP HELPING ICE Sep 14 '15 at 16:43
  • It works, great. I was trying to understand which part of the GIT documentation talked about this, but could not. May you elaborate pls? – Meglio Dec 04 '17 at 11:57
  • @Meglio `git archive --help`, let me know if you cannot find something (and then please be as specific as possible) – janos Dec 04 '17 at 13:56
  • 1
    Note that if the current working directory is not the root of the repo, then git will archive from working directory (and descendants) only. To change this, use the `:` notation above - i.e. just `HEAD:`; `HEAD:/` and `HEAD:.` don't work. – user234461 Dec 06 '17 at 17:20
  • For some reason though, if you use this, it does ignore the `export-ignore` statements in your `.gitattributes`. Even if the file is included in the dir you export and even if you use `--worktree-attributes`. – rugk Oct 14 '18 at 15:03
  • Okay, asked a separate question for using this here together with `.gitattributes` "export-ignore", which did not really work: [How to ignore files/directories in “git archive” and only create an archive of a subdirectory?](https://stackoverflow.com/questions/52804334/how-to-ignore-files-directories-in-git-archive-and-only-create-an-archive-of-the) – rugk Oct 14 '18 at 15:45