让git忽略生成的文件 [英] Make git ignore generated files

查看:98
本文介绍了让git忽略生成的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个有大量文件的项目,这些文件在处理时会生成其他文件。没有必要跟踪生成的文件,实际上,在某些情况下,您不希望跟踪它们,因为这会浪费空间。我知道这是 .gitignore 文件的用途。问题是,我找不到在文件上使用模式匹配的好方法,因为代表还包含符合相同模式的常规用户编辑文件。例如,一个repo将有多个用户编辑的 .tex 文件,但也有一些 .tex 由knitr格式 .Rnw 文件生成的文件。大多数 .pdf 文件是从 .tex 文件自动生成的,但有些是用户创建的。这些数据将包含在TeX文档中,并且应该被跟踪。



只要有 blah.tex 的话, code> blah.Rnw 放在同一个目录中,并且只有当 bluh时忽略 bluh.pdf .tex bluh.Rnw 在同一个目录中?

解决方案添加所有后缀(例如 .pdf

c>)到 .gitignore 作为 *。pdf git add -f 那些应该版本化的 version-this-one.pdf git 会跟踪那些(需要一点小心)。 *。pdf 实际上是一个整数,所以你可以排除那些hames以大写字母开头的PDF,其格式为 [AZ] *。pdf
  • 如上所述,但添加例如 *。pdf 用于所有PDF,并且!version-this-one.pdf 从排除的集合中排除一个也可以在这里使用一个模式)

  • 将生成的文件的完整列表变为 .gitignore 。需要保持最新版本(不是太糟糕,如果有新版本出现, git 会认为它没有跟踪,只需添加它即可)。

  • 混合匹配:根据需要使用上述方法获取不同的文件名模式
  • 每个目录 .gitignore git 首先在该目录的 .gitignore 中搜索,如果在父目录中找不到, ,这会停止搜索。



  • 查看 gitignore(5)


    I have several projects that have a large number of files that, when processed, generate other files. There is no need to track the generated files, in fact, in some cases you do not want to track them as it will be a waste of space. I understand that that is something the .gitignore file is for. The problem is, I cannot find a good way to use pattern matching on the files, because the reps also contain regular user editted files that conform to the same patterns. For example, a repo will have a number of .tex files that are user editted, but also a number of .tex files that are produced by knitr form .Rnw files. Most .pdf files are auto generated from .tex files, but some are user created .pdf figures that will be included in TeX documents, and should be tracked.

    What would be a good way to make git for example ignore a file blah.tex only if there is blah.Rnw in the same directory, and to ignore bluh.pdf only if there is bluh.tex or bluh.Rnw in the same directory?

    解决方案

    Several simple solutions:

    • Add all suffixes (e.g. .pdf) to .gitignore as *.pdf, and git add -f those version-this-one.pdf that should be versioned, git will keep track of those (need a bit of care). The *.pdf is really a glob, so you could exclude, say, PDFs whose hames start with uppercase by [A-Z]*.pdf.
    • As above, but add e.g. *.pdf for all PDFs, and !version-this-one.pdf to exclude one from the excluded set (can also use a pattern here)
    • Make a complete list of the generated files into .gitignore. Need to keep it up to date (not too bad, if a new one shows up, git will consider it untracked, just add it then).
    • Mix and match: Use the above methods as you want for different filename patterns
    • Per directory .gitignore: git searches first in this directory's .gitignore, if not found in the parent's, ... As you can exclude patterns with !, this stops a search there.

    Check out gitignore(5).

    这篇关于让git忽略生成的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆