>_ IzzyLab

Hands-on task

This task is step 21 of the course “Git: rewriting and rescuing history”

Exclude a directory from git archive

medium~7 min

In /root/repo there is a file build.txt and a directory secret/ with a file secret/keys.txt.

Make it so the secret/ directory doesn't get into the archive created via git archive, while build.txt stays in the archive and secret/ itself is not removed from the repository. To do this, add a .gitattributes file to the repository with the export-ignore attribute for secret/ and commit it (the archive reads attributes from the specified revision, so the changes must be in a commit).

What you'll practise:
  • git archive
  • gitattributes
  • export-ignore
  • release export

Topic breakdown

git archive assembles a tarball from the contents of the specified revision, not from the working directory. Which paths to exclude from the export is set by the export-ignore attribute in the .gitattributes file (a line of the form <path> <attribute>) — the standard way not to drag service and secret files into a release. An important nuance: attributes are read from the tree of the same revision being archived, so .gitattributes must be committed — edits only in the working directory won't affect the archive.

What's checked

  1. secret/ is absent from the archive
  2. build.txt is present in the archive
  3. secret/ remained in the repository
  4. .gitattributes is committed

What it looks like