git .gitignore 到底是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27850222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:48:07  来源:igfitidea点击:

What is .gitignore exactly?

gitgithubgitignore

提问by Zahachos

I just created a Github repository and was wondering what the .gitignorefile was for. I started by not creating one, but added one due to the fact that most repositories have one. Do I need to have one? Can/do I just ignore it, or does it have a use? I did some research on the subject but couldn't find a concrete explanation.

我刚刚创建了一个 Github 存储库,并想知道该.gitignore文件的用途。我一开始没有创建一个,但由于大多数存储库都有一个,因此添加了一个。我需要有一个吗?我可以/我是否可以忽略它,或者它有用吗?我对该主题进行了一些研究,但找不到具体的解释。

采纳答案by Mureinik

.gitignoretells git which files (or patterns) it should ignore. It's usually used to avoid committing transient files from your working directory that aren't useful to other collaborators, such as compilation products, temporary files IDEs create, etc.

.gitignore告诉 git 它应该忽略哪些文件(或模式)。它通常用于避免从您的工作目录提交对其他协作者无用的临时文件,例如编译产品、IDE 创建的临时文件等。

You can find the full details here.

您可以在此处找到完整的详细信息。

回答by Andy Lester

It's a list of files you want git to ignore in your work directory.

它是您希望 git 在您的工作目录中忽略的文件列表。

Say you're on a Mac and you have .DS_Store files in all your directories. You want git to ignore them, so you add .DS_Store as a line in .gitignore. And so on.

假设您使用的是 Mac,并且您的所有目录中都有 .DS_Store 文件。您希望 git 忽略它们,因此您将 .DS_Store 添加为 .gitignore 中的一行。等等。

The git docs will tell you all you need to know: http://git-scm.com/docs/gitignore

git 文档会告诉你所有你需要知道的:http: //git-scm.com/docs/gitignore

回答by programmer_of_the_galaxies

When you are doing a commit you do not want accidentally include temporary files or build specific folders. Hence use a .gitignorelisting out items you want to ignore from committing.

当您进行提交时,您不希望意外包含临时文件或构建特定文件夹。因此,请使用.gitignore列出您希望在提交时忽略的项目。

Also, importantlygit statusis one of the most frequently used command where you want git statusto list out the files that have been modified.

此外,重要的git status是要git status列出已修改的文件的最常用命令之一。

You would want your git statuslist look cleanfrom unwanted files. For instance, I changed a.cpp, b.cpp, c.cpp, d.cpp & e.cppI want my git statusto list the following:

希望您的git status列表看起来没有不需要的文件。例如,我更改了a.cpp, b.cpp, c.cpp, d.cpp & e.cpp我希望git status列出以下内容:

git status
a.cpp
b.cpp
c.cpp
d.cpp
e.cpp

I dont want git statusto list out changed files like this with the intermediary object files & files from the build folder

我不想git status用构建文件夹中的中间对象文件和文件列出这样的更改文件

git status
a.cpp
b.cpp
c.cpp
d.cpp
e.cpp
.DS_Store
/build/program.o
/build/program.cmake

Hence, to get myself free from git statusto list out these intermediate temporary files & accidentally committing them into the repo, I should create a .gitignorewhich everyone does. All I need to do list out the files & folders in the .gitignorethat I want to exclude from committing.

因此,为了让自己免于git status列出这些中间临时文件.gitignore并不小心将它们提交到存储库中,我应该创建一个每个人都这样做的文件。我需要做的就是列出.gitignore我想从提交中排除的文件和文件夹。

Following is my .gitignoreto avoid committing unnecessary files

以下是我.gitignore为了避免提交不必要的文件

/*.cmake
/*.DS_Store
/.user
/build

回答by imk

There are files you don't want Git to check in to. Git sees every file in your working copy as one of three things:

有些文件您不希望 Git 签入。Git 将工作副本中的每个文件视为以下三件事之一:

  1. tracked - a file which has been previously staged or committed;
  2. untracked - a file which has not been staged or committed; or
  3. ignored - a file which Git has been explicitly told to ignore.
  1. 已跟踪 - 先前已暂存或提交的文件;
  2. 未跟踪 - 尚未暂存或提交的文件;或者
  3. 忽略 - Git 已被明确告知忽略的文件。

Ignored files are usually built artifacts and machine-generated files that can be derived from your repository source or should otherwise not be committed. Some common examples are:

忽略的文件通常是构建的工件和机器生成的文件,它们可以从您的存储库源中派生,否则不应提交。一些常见的例子是:

  • dependency caches, such as the contents of /node_modulesor /packages
  • compiled code, such as .o, .pyc, and .classfiles
  • build output directories, such as /bin, /out, or /target
  • files generated at runtime, such as .log, .lock, or .tmp
  • hidden system files, such as .DS_Storeor Thumbs.db
  • personal IDE config files, such as .idea/workspace.xml
  • 依赖缓存,例如/node_modules/packages
  • 编译后的代码,比如.o.pyc.class文件
  • 构建输出目录,例如/bin, /out, 或/target
  • 运行时生成的文件,例如.log, .lock, 或.tmp
  • 隐藏的系统文件,例如.DS_StoreThumbs.db
  • 个人 IDE 配置文件,例如 .idea/workspace.xml

Ignored files are tracked in a special file named .gitignorethat is checked in at the root of your repository. There is no explicit git ignore command: instead the .gitignorefile must be edited and committed by hand when you have new files that you wish to ignore. .gitignorefiles contain patterns that are matched against file names in your repository to determine whether or not they should be ignored. Here is a sample.gitignorefile. For more details look at this link

忽略的文件在一个名为的特殊文件中进行跟踪,该文件.gitignore在存储库的根目录中签入。没有明确的 git ignore 命令:相反,.gitignore当您有要忽略的新文件时,必须手动编辑和提交文件。.gitignorefiles 包含与存储库中的文件名匹配的模式,以确定是否应该忽略它们。这是一个示例.gitignore文件。有关更多详细信息,请查看此链接

回答by BKSpurgeon

Main purpose of .gitignore – so you avoid adding irrelevant files etc. Git

.gitignore 的主要目的——避免添加不相关的文件等。 Git

I have a lot of personal notes / scribbles in certain repositories: they are useful to me but not for anyone else. I would not want it uploaded to github because it would simply confuse everyone who read it. The good thing is that I can ask git to "ignore" those files. The only cost to this approach is that I will not be able to recover those notes if my computer crashes etc.

我在某些存储库中有很多个人笔记/涂鸦:它们对我有用,但对其他人没有用。我不希望它上传到 github,因为它只会混淆阅读它的每个人。好消息是我可以让 git “忽略”这些文件。这种方法的唯一成本是,如果我的计算机崩溃等,我将无法恢复这些笔记。