Best practice for using multiple .gitignore files

后端 未结 1 1261
我在风中等你
我在风中等你 2021-02-03 18:36

There exists a collection of useful .gitignore files at https://github.com/github/gitignore. Every ignores file there has an extension .gitignore, e.g.

1条回答
  •  傲寒
    傲寒 (楼主)
    2021-02-03 19:26

    To clarify: a projects gitignore file is called .gitignore


    Background:

    A .gitignore refers to the directory that it's in, which is either the top level or descendent of a directory with a .git repository, i.e. a ".git/" directory.

    There can be multiple .gitignore files in any sub directories but the Best Practice is to have one .gitignore in a given projects root and have that file reference sub-directories as necessary, e.g. images/yearly/recent Otherwise it is be tricky to know "which" .gitignore file to look at to find something that's being ignored. Given that you can use patterns as file names that could be pretty tricky!

    I also recommend avoiding using a global .gitignore file which applies to all projects on your machine, although you might keep a template around for using with new projects. The main consideration here is that your .gitignore will be different from other developers (which may or may not exist) and so the result is undetermined. One example of an exception to this is using a global .gitignore file for IDE files that I don't want in any project that I open on my machine so I use a global .gitigore with an entry for .idea/ files (rubyMine).


    The intent of the templates you see listed is that normally you are writing the code for a given file in a specific language. Given this, a template that is based on the language is frequently sufficient.

    If there are multiple languages in the code base, then used you will need to combine multiple .gitignore's for those languages, which can be done in a multitude of ways such as:

    cat .gitignore1 .gitignore2 > .gitignore # if .gitignore doesn't exist yet
    cat .gitignore1 >> .gitignore # Add to it if it already exists
    paste .gitignore1 .gitignore # Add to it if it already exists
    

    Hot(ish) off the press (summer 2014):

    Gitignorer is a simple utility that aids in the creation of .gitignore files. It pulls specific (specified) .gitignore templates, with common files to exclude, from github.com/github/gitignore, mashes them together, and saves them to a .gitignore in the current directory.

    Example usage:

    gitignorer create c java python
    

    Gitignorer is currently available in the AUR over at https://aur.archlinux.org/packages/gitignorer/ and on GitHub at https://github.com/zachlatta/gitignorer


    0 讨论(0)
提交回复
热议问题