36

I have some files like vim.gitignore, SVN.gitignore and CVS.gitignore (spread around on my hard disk).

Can I simply include these gitignore files in a .gitignore file in a new Git project?

Edit: I have a global ignore file already.
I just want to ignore different files in different types of projects, is this possible?

VoY
  • 5,479
  • 2
  • 37
  • 45
HaveF
  • 2,995
  • 2
  • 26
  • 36

4 Answers4

12

You could:

  • Have a template repo with:
    • those xxx.gitignore files in it:
    • a .gitignore file with "xxx-gitignore-xxx" in it (in other word, with a content you can easily identify
    • a .gitattribute filter driver

(for each new repo, you clone it and can start with those files already there.
Then you remove the remote 'origin', or replace it by whatever remote repo you want to push to)

smudge content filter
(image shown in "Customizing Git - Git Attributes", from "Pro Git book")

On any checkout of your repo, the filter driver will, through the smudge script:

  • recognize the content of the .gitignore file
  • check if the xxx.gitignore content isn't already there (by looking for a specific string which only those files have)
  • if their content isn't there, it will append the content of those xxx.gitignore files
  • if the content is already up-to-date, it wouldn't modify the .gitignore.

Note that having a identifiable content is key here, since a filter driver script doesn't have the name/path of the file it filters.

It is a bit convoluted, but seems to be the only way to implement the "include" feature you want.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
7

The closes you will get is the global ignore file:

git config --global core.excludesfile ~/.gitignore

which is of course like having a .gitignore in the repo, but applicable to all repos on your system ( and not propagated to external clones.)

manojlds
  • 290,304
  • 63
  • 469
  • 417
  • 4
    thanks! But I have global ignore file already. I just want to ignore different files in different type of project, could be possible? – HaveF Aug 10 '11 at 03:09
5

I run the a project that hosts the largest amount of gitignore templates and it's extremely easy to use.

This doesn't allow anyone to include sub-templates, but it does generate one large template from individual git templates.

Joe
  • 1,320
  • 13
  • 11
1

You could also use the @slidewave/gitignore-include NPM package to accomplish the task.

Example:

## <include href="path/to/vim.gitignore">
## </include>
## <include href="path/to/SVN.gitignore">
## </include>
## <include href="path/to/CVS.gitignore">
## </include>
## <include href="https://github.com/github/gitignore/raw/main/Global/macOS.gitignore">
## </include>
## <include href="https://github.com/github/gitignore/raw/main/Global/VisualStudioCode.gitignore">
## </include>

# Any local overrides go here.

Then either

  1. execute npx -p @slidewave/gitignore-include giismudge .gitignore to bring everything together,
  2. hook up the git hooks to smudge your ignore files on commit, or
  3. set up a .gitattribute filter driver as noted in VonC's answer but using giismudge to do the work.

Caveat: anyone, including you, who needs to re-smudge the ignore files will need to have the same source files at the same paths in order to make this work. So I recommend placing your source gitignore files into a public repository and using the raw URLs to them similar to what I showed for the macOS and VSCode include directives.

Note that I'm the author of the tool, so I'm a bit biased and am responding as a form of advertising the tool. :)

Ricky C
  • 53
  • 8
  • Interesting tool, upvoted. Can the step 2 and 3 automated in a "setup" script? – VonC Dec 22 '22 at 07:02
  • 1
    I don’t know about the general case, but under NPM/yarn/etc the way I do that is using Husky and Lint-Staged. Documentation for doing so is on the site’s Readme, and the project also uses that same setup. – Ricky C Dec 23 '22 at 14:07