TL;DR: The git archive
command seems to only either archive a single subdirectory or adhere to .gitattributes
and exclude files/directories from the resulting ZIP, but cannot do both.
Say I have this directory structure in my git archive (simplified for reasability):
.
├── assets
├── build
├── CONTRIBUTING.md
├── CONTRIBUTORS
├── LICENSE.md
├── README.md
├── scripts
├── src
│ ├── background
│ ├── common
│ ├── icons
│ ├── _locales
│ ├── manifest.json
│ ├── options
│ ├── popup
│ └── tests
└── tests -> src/tests
Now I want to get an ZIP (or tar) archive with the following content:
- content of
src
directory only, as this is the source code I want to archive. Without thesrc
directly, itself, however. - inside of these directories I may want to ignore more folders and files. E.g.
tests
needs to be ignored, obviously.
So in the end, the archive should e.g. look like that:
.
├── background
├── common
├── icons
├── _locales
├── manifest.json
├── options
└── popup
Now, I thought git archive
is a great tool for that. You can archive a subdirectory only and use .gitattributes
files with export-ignore
to exclude directories and files wherever I want.
The aim is to also use .gitattributes
recursively inside multiple subfolders, so I can also always exclude single files from the archive without knowing the upper folder structure. (That's useful for included libraries e.g.)
I also noticed you may need to add the parameter --worktree-attributes
so git picks up the .gitattributes
files everywhere, where it finds it. And I have use **
to exclude subdirectories. That's fine.
So (for testing here) I tried this:
$ git archive --worktree-attributes --format=tar HEAD:src | tar t
So only archiving the subdirectory works without any problems, but excludes files did not work.
E.g. I put this .gitattributes
into the src
directory:
tests export-ignore
tests/** export-ignore
Now, running the command mentioned above, it still includes the tests
dir content.
However, if I run git archive --worktree-attributes --format=tar HEAD | tar t
, i.e. not archive the subdirectory, the list ends with:
src/popup/[…]
tests
This means it excludes the content of the tests
dir. For some reason, it did not exclude the dir itself, but that may be another issue on my side.
In contrast, if I put the follwing file into the root directory, the whole src/tests
dir is excluded (although I had said they semantically mean the same):
src/tests export-ignore
src/tests/** export-ignore
My git version: 2.17.2
Fedora 28
Edit: I noticed now it somehow seems to work when I put the .gitattributes
file into the root dir, but mention the file paths relative to the src
dir. I.e. doing this (also tested with file exclusion):
tests export-ignore
tests/** export-ignore
manifest.json export-ignore
…somehow makes it work with the src
archive command. Now, it does not work with archiving the whole dir anymore (but that is okay).
However, if I put this file anywhere else (like in the src
dir) it again does not work…
And as my aim is to have .gitattributes
files in subdirectories to exclude specific stuff in these directories, this still does not work as expected.