Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save tangoabcdelta/2324ce97066ffadcca3a6f66b1aaded4 to your computer and use it in GitHub Desktop.
Save tangoabcdelta/2324ce97066ffadcca3a6f66b1aaded4 to your computer and use it in GitHub Desktop.
Generate zip archive of a git repository ignoring the files listed in `.gitignore`:

I have a git repo which compiles into a dist folder and generates a bunch binaries (executables). The binaries and dist folders are .gitignore-ed and hence, are not included in the repo. But I want to distribute a source + binaries snapshot zipfile. I want them to contain:

a) all the sources b) all the binaries c) the dist folder (so that they can tweak it) d) not the .git/ directory, not the hidden files like .cache or node_modules/ etc.

Generate zip archive of a git repository ignoring the files listed in .gitignore:

git archive -o $@.zip HEAD
  • To exclude files and directories from git archive, you can also use the .gitattributes file.
    1. Create a file named .gitattributes in your project root (if it doesn't exist).

    2. Edit the file: Each exclusion should have a file pattern followed by "export-ignore":

      .gitattributes export-ignore .gitignore export-ignore /mytemp export-ignore

    3. git-ignore the .gitattributes file itself

    4. Commit the .gitattributes file (otherwise, git archive won't pick up the settings)

Then proceed with the natural way of creating a snapshot zip:

git archive -o archive.zip 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment