为什么要使用.gitignore? [英] Why should I use .gitignore?

查看:116
本文介绍了为什么要使用.gitignore?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,每当我创建一个我希望Git跟踪的文件时,我都只是将其添加到索引中.如果我不将其添加到索引中,Git将不会看到它".那么,为什么不使用.gitignore文件而不是不将其添加到索引呢?

Currently, whenever I create a file that I'd like Git to track I simply add it to the index. If I don't add it to the index Git would not "see it". So why should I use a .gitignore file, as opposed to just not adding it to the index?

推荐答案

通常,您会发现,随着项目的扩大,将每个文件一个接一个地添加可能会变得乏味.

You'll generally find that adding each and every file one by one can become tedious when your project becomes bigger.

在Java中,您可能最终得到许多不想添加到存储库中的.class文件.同样,您可能最终会在C中获得许多.o文件,而在Python中最终会得到.pyc文件.

In Java, you may end up with many .class files that you wouldn't want to add to your repository. Similarly, you may end up with many .o files in C, or .pyc in Python (for example).

.gitignore文件中具有*.class*.o*.pyc之类的模式,使您可以一劳永逸地忽略这些文件.随后的git status运行(或者如果您使用的是GUI,则类似)不会告诉您这些是未跟踪的新文件,从而使您可以专注于真正新的和未跟踪的文件.

Having patterns like *.class, *.o, *.pyc in your .gitignore files allows you to ignore those files once and for all. Subsequent git status runs (or similar if you're using a GUI) will not tell you these are new, untracked files, thereby letting you focus on the files that are really new and noticeably untracked.

如果您添加整个目录(例如git add myproject),这也很有用:这可以让您完全忽略一类文件.

This can also be useful if you add an entire directory (e.g. git add myproject): this lets you ignore a category of files altogether.

这篇关于为什么要使用.gitignore?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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