Git: How to ignore/specify files for *checkout*
So I wanted to ship my code without the test files to the production server.
Easiest way for me was just to remove all the test files with: rsync --exclude 'tests'
after unpacking the archive.
Git's Sparse-Checkout
If you only want to checkout a portion of your repository, you can use git's sparse-checkout option which has powerful include/exclude rules.
The following StackOverflow question has some helpful instructions:
GIT checkout except one folder
But as a summary:
Enable the
sparseCheckout
option:git config core.sparseCheckout true
Create the file called
.git/info/sparse-checkout
containing:/* !node_modules
which effectively means include everything except the
node_modules
directory when checking out a repository into the working directory.
If you want to package up files for deployment, you probably don't need - or want - the repo itself. This is exactly what git archive
is for. A couple examples from the manpage (linked):
git archive --format=tar --prefix=junk/ HEAD | (cd /var/tmp/ && tar xf -)
Create a tar archive that contains the contents of the latest commit on the current branch, and extract it in the /var/tmp/junk directory.
git archive --format=tar --prefix=git-1.4.0/ v1.4.0 | gzip > git-1.4.0.tar.gz
Create a compressed tarball for v1.4.0 release.
You ought to be able to get it to do exactly what you want, with the help of the export-ignore
attribute:
export-ignore
Files and directories with the attribute export-ignore won’t be added to archive files. See gitattributes(5) for details.
For example, to exclude the directory private
and the files mine.txt
and secret.c
, you could put in the file .gitattributes
:
private/ export-ignore
secret.c export-ignore
Just like gitignore files, you can put those anywhere in your repository, and they'll operate from that directory, but starting from the top level is a good bet.