Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I include other .gitignore file in a .gitignore file? (like #include in c-like languages)

Tags:

git

gitignore

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?

like image 301
HaveF Avatar asked Aug 10 '11 02:08

HaveF


People also ask

Can I add multiple Gitignore files?

A . gitignore file is a plain text file where each line contains a pattern for files/directories to ignore. Generally, this is placed in the root folder of the repository, and that's what I recommend. However, you can put it in any folder in the repository and you can also have multiple .

Can you add Gitignore to Gitignore?

Yes you can; you still see it in edited files because it is still tracked by git, and files tracked by git are always marked as modified even if they are in . gitignore.


1 Answers

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.

like image 187
VonC Avatar answered Sep 20 '22 22:09

VonC