如何阻止.gitignore出现在未跟踪文件列表中?

时间:2021-05-21 15:01:17

I just did a git init on the root of my new project.

我刚刚在我的新项目的根目录上做了一个git init。

Then I created a .gitignore file.

然后我创建了一个.gitignore文件。

Now, when I type git status, .gitignore file appears in the list of untracked files. Why is that?

现在,当我输入git status时,.gitignore文件出现在未跟踪文件列表中。这是为什么?

19 个解决方案

#1


The .gitignore file should be in your repository, so it should indeed be added and committed in, as git status suggests. It has to be a part of the repository tree, so that changes to it can be merged and so on.

.gitignore文件应该在您的存储库中,因此它确实应该被添加并提交,如git status所示。它必须是存储库树的一部分,因此可以合并对它的更改等。

So, add it to your repository, it should not be gitignored.

因此,将它添加到您的存储库,它不应该被gitignored。

If you really want you can add .gitignore to the .gitignore file if you don't want it to be committed. However, in that case it's probably better to add the ignores to .git/info/exclude, a special checkout-local file that works just like .gitignore but does not show up in "git status" since it's in the .git folder.

如果您真的想要,可以将.gitignore添加到.gitignore文件中,如果您不希望它被提交。但是,在这种情况下,最好将忽略添加到.git / info / exclude,这是一个特殊的checkout-local文件,其工作方式与.gitignore类似但不会显示在“git status”中,因为它位于.git文件夹中。

See also https://help.github.com/articles/ignoring-files

另请参见https://help.github.com/articles/ignoring-files

#2


If you want to store the list of ignored files outside of your Git tree, you can use the .git/info/exclude file. It is applied only to your checkout of the repo.

如果要将忽略文件列表存储在Git树之外,可以使用.git / info / exclude文件。它仅适用于您的结帐回收。

#3


You could actually put a line ".gitignore" into your ".gitignore" file. This would cause the ".gitignore" file to be ignored by git. I do not actually think this is a good idea. I think the ignore file should be version controlled and tracked. I'm just putting this out there for completeness.

您实际上可以在“.gitignore”文件中添加一行“.gitignore”。这将导致git忽略“.gitignore”文件。我实际上并不认为这是个好主意。我认为忽略文件应该受版本控制和跟踪。我只是为了完整而把它放在那里。

#4


You can also have a global user git .gitignore file that will apply automatically to all your repos. This is useful for IDE and editor files (e.g. swp and *~ files for Vim). Change directory locations to suite your OS

您还可以拥有一个全局用户git .gitignore文件,该文件将自动应用于您的所有回购。这对IDE和编辑器文件很有用(例如,Vim的swp和*〜文件)。更改目录位置以适应您的操作系统

  1. Add to your ~/.gitconfig file

    添加到〜/ .gitconfig文件

    [core]
    excludesfile = /home/username/.gitignore
    
  2. Create a ~/.gitignore file with file patterns to be ignored

    创建一个带有要忽略的文件模式的〜/ .gitignore文件

  3. Save your dot files in another repo so you have a backup (optional).

    将您的点文件保存在另一个仓库中,以便备份(可选)。

Any time you copy, init or clone a repo your global gitignore file will be used as well.

每次复制,初始化或克隆存储库时,也会使用全局gitignore文件。

#5


If someone has already added a .gitignore to your repo, but you want to make some changes to it and have those changes ignored do the following:

如果有人已经在您的仓库中添加了.gitignore,但您想对其进行一些更改并忽略这些更改,请执行以下操作:

git update-index --assume-unchanged .gitignore

git update-index --assume-unchanged .gitignore

Source.

#6


After you add the .gitignore file and commit it, it will no longer show up in the "untracked files" list.

添加.gitignore文件并提交后,它将不再显示在“未跟踪文件”列表中。

git add .gitignore
git commit -m "add .gitignore file"
git status

#7


Just incase someone else has the same pain we had. We wanted to exclude a file that had already been committed.

只是让别人有同样的痛苦。我们想要排除已经提交的文件。

This post was way more useful: working with .git/info/exclude too late

这篇文章更有用:使用.git / info / exclude太晚了

Specifically what you need to ignore a file is actually use the command git remove See git rm (http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

具体你需要忽略一个文件实际上是使用命令git remove参见git rm(http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

you test it by going

你去试试吧

git rm --dry-run *.log
(if you say wanted to exclude all the log files)

git rm --dry-run * .log(如果你想要排除所有日志文件)

this will output what would be excluded if you ran it.

这将输出运行它时将被排除的内容。

then

you run it by going

你去吧

git rm *.log
(or whatever filename path / expression you want to)

git rm * .log(或者你想要的任何文件名路径/表达式)

Then add a *.log line to your .gitignore file.

然后在.gitignore文件中添加* .log行。

#8


First of all, as many others already said, your .gitignore should be tracked by Git (and should therefore not be ignored). Let me explain why.

首先,正如许多其他人已经说过的那样,你的.gitignore应该由Git跟踪(因此不应该被忽略)。让我解释一下原因。

(TL;DR: commit the .gitignore file, and use a global .gitignore to ignore files that are created by your IDE or operating system)

(TL; DR:提交.gitignore文件,并使用全局.gitignore忽略由IDE或操作系统创建的文件)

Git is, as you probably already know, a distributed version control system. This means that it allows you to switch back and forth between different versions (even if development has diverged into different branches) and it also allows multiple developers to work on the same project.

正如您可能已经知道的那样,Git是一个分布式版本控制系统。这意味着它允许您在不同版本之间来回切换(即使开发已经分散到不同的分支),并且它还允许多个开发人员在同一个项目上工作。

Although tracking your .gitignore also has benefits when you switch between snapshots, the most important reason for committing it is that you'll want to share the file with other developers who are working on the same project. By committing the file into Git, other contributers will automatically get the .gitignore file when they clone the repository, so they won't have to worry about accidentally committing a file that shouldn't be committed (such as log files, cache directories, database credentials, etc.). And if at some point the project's .gitignore is updated, they can simply pull in those changes instead of having to edit the file manually.

虽然在快照之间切换时跟踪.gitignore也有好处,但提交它的最重要原因是您希望与正在处理同一项目的其他开发人员共享该文件。通过将文件提交到Git,其他贡献者将在克隆存储库时自动获取.gitignore文件,因此他们不必担心意外提交不应提交的文件(例如日志文件,缓存目录,数据库凭证等)。如果在某个时刻项目的.gitignore被更新,他们可以简单地提取这些更改,而不必手动编辑文件。

Of course, there will be some files and folders that you'll want to ignore, but that are specific for you, and don't apply to other developers. However, those should not be in the project's .gitignore. There are two other places where you can ignore files and folders:

当然,会有一些您想要忽略的文件和文件夹,但这些文件和文件夹特定于您,并不适用于其他开发人员。但是,那些不应该在项目的.gitignore中。您还可以在其他两个位置忽略文件和文件夹:

  • Files and folders that are created by your operating system or IDE should be placed in a global .gitignore. The benefit is that this .gitignore is applied to all repositories on your computer, so you don't have to repeat this for every repository. And it's not shared with other developers, since they might be using a different operating system and/or IDE.
  • 由操作系统或IDE创建的文件和文件夹应放在全局.gitignore中。好处是这个.gitignore应用于您计算机上的所有存储库,因此您不必为每个存储库重复此操作。并且它不与其他开发人员共享,因为他们可能使用不同的操作系统和/或IDE。

  • Files that don't belong in the project's .gitignore, nor in the global .gitignore, can be ignored using explicit repository excludes in your_project_directory/.git/info/exclude. This file will not be shared with other developers, and is specific for that single repository
  • 使用your_project_directory / .git / info / exclude中的显式存储库排除,可以忽略不属于项目的.gitignore和全局.gitignore的文件。此文件不会与其他开发人员共享,并且特定于该单个存储库

#9


The idea is to put files that are specific to your project into the .gitignore file and (as already mentioned) add it to the repository. For example .pyc and .o files, logs that the testsuite creates, some fixtures etc.

我们的想法是将特定于项目的文件放入.gitignore文件中(如前所述)将其添加到存储库中。例如.pyc和.o文件,测试套件创建的日志,一些固定装置等。

For files that your own setup creates but which will not necessarily appear for every user (like .swp files if you use vim, hidden ecplise directories and the like), you should use .git/info/exclude (as already mentioned).

对于您自己的设置创建但不一定为每个用户显示的文件(如.swp文件,如果您使用vim,隐藏的ecplise目录等),您应该使用.git / info / exclude(如前所述)。

#10


Of course the .gitignore file is showing up on the status, because it's untracked, and git sees it as a tasty new file to eat!

当然.gitignore文件显示在状态上,因为它没有跟踪,git认为它是一个可口的新文件!

Since .gitignore is an untracked file however, it is a candidate to be ignored by git when you put it in .gitignore!

因为.gitignore是一个未经跟踪的文件,但是当你把它放入.gitignore时它是被git忽略的候选者!

So, the answer is simple: just add the line:

所以,答案很简单:只需添加以下行:

.gitignore # Ignore the hand that feeds!

to your .gitignore file!

到您的.gitignore文件!

And, contrary to August's response, I should say that it's not that the .gitignore file should be in your repository. It just happens that it can be, which is often convenient. And it's probably true that this is the reason .gitignore was created as an alternative to .git/info/exclude, which doesn't have the option to be tracked by the repository. At any rate, how you use your .gitignore file is totally up to you.

而且,与August的回复相反,我应该说,并不是.gitignore文件应该在你的存储库中。它恰好发生,这通常很方便。而且这可能是因为这就是为什么.gitignore被创建为.git / info / exclude的替代品,它没有存储库可以跟踪的选项。无论如何,你如何使用.gitignore文件完全取决于你。

For reference, check out the gitignore(5) manpage on kernel.org.

有关参考,请查看kernel.org上的gitignore(5)联机帮助页。

#11


Watch out for the following "problem" Sometimes you want to add directories but no files within those directories. The simple solution is to create a .gitignore with the following content:

注意以下“问题”有时你想在这些目录中添加目录但没有文件。简单的解决方案是创建一个包含以下内容的.gitignore:

*

This seams to work fine until you realize that the directory was not added (as expected to your repository. The reason for that is that the .gitignore will also be ignored, and thereby the directory is empty. Thus, you should do something like this:

这个接缝工作正常,直到你意识到没有添加目录(正如预期的那样存储库。原因是.gitignore也将被忽略,因此目录是空的。因此,你应该做这样的事情:

*
!.gitignore

#12


This seems to only work for your current directory to get Git to ignore all files from the repository.

这似乎只适用于您当前的目录,以使Git忽略存储库中的所有文件。

update this file

更新此文件

.git/info/exclude 

with your wild card or filename

用你的外卡或文件名

*pyc
*swp
*~

#13


In my case, I want to exclude an existing file. Only modifying .gitignore not work. I followed these steps:

就我而言,我想要排除现有文件。只修改.gitignore不起作用。我按照以下步骤操作:

git rm --cached dirToFile/file.php
vim .gitignore
git commit -a

In this way, I cleaned from cache the file that I wanted to exclude and after I added it to .gitignore.

通过这种方式,我从缓存中清除了我想要排除的文件,并在将其添加到.gitignore之后。

#14


Navigate to the base directory of your git repo and execute the following command:

导航到git仓库的基本目录并执行以下命令:

echo '\\.*' >> .gitignore

All dot files will be ignored, including that pesky .DS_Store if you're on a mac.

所有点文件都将被忽略,包括那个讨厌的.DS_Store,如果你在mac上。

#15


If you've already checked in .gitignore and you want to ignore modifications to it, check out this answer:

如果您已经签入.gitignore并且想要忽略对它的修改,请查看以下答案:

Try using this command:

尝试使用此命令:

git update-index --assume-unchanged FILENAME_TO_IGNORE

To reverse it (if you ever want to commit changes to it), use:

要反转它(如果您想要对其进行更改),请使用:

git update-index --no-assume-unchanged

UPDATE:

Here's how to list 'assume unchanged' files under current directory:

以下是如何列出当前目录下的“假设未更改”文件:

git ls-files -v | grep -E "^[a-z]"

As the -v option will use lowercase letters for 'assume unchanged' files.

由于-v选项将使用小写字母表示“假定未更改”文件。

#16


It is quite possible that an end user wants to have Git ignore the ".gitignore" file simply because the IDE specific folders created by Eclipse are probably not the same as NetBeans or another IDE. So to keep the source code IDE antagonistic it makes life easy to have a custom git ignore that isn't shared with the entire team as individual developers might be using different IDE's.

最终用户很可能希望让Git忽略“.gitignore”文件,因为Eclipse创建的IDE特定文件夹可能与NetBeans或其他IDE不同。因此,为了保持源代码IDE的对抗性,它使生活变得容易,因为单个开发人员可能使用不同的IDE,因此无法与整个团队共享自定义git忽略。

#17


.gitignore is about ignoring other files. git is about files so this is about ignoring files. However as git works off files this file needs to be there as the mechanism to list the other file names.

.gitignore是关于忽略其他文件。 git是关于文件的,所以这是关于忽略文件。但是,当git处理文件时,此文件需要作为列出其他文件名的机制。

If it were called .the_list_of_ignored_files it might be a little more obvious.

如果它被称为.the_list_of_ignored_files,它可能会更加明显。

An analogy is a list of to-do items that you do NOT want to do. Unless you list them somewhere is some sort of 'to-do' list you won't know about them.

类比是您不想做的待办事项列表。除非你在某个地方列出某些“待办事项”列表,否则你将无法了解它们。

#18


I think that there are situations where ignoring the .gitignore is very useful. For instance, when you have multiple teams or a large team working on the same codebase. In that case, you need to have certain conventions, one of those convention is regarding what is ignored at the git repo. It is usually about ignoring files and directories created by IDE or OS, some generated logs, etc.

我认为有些情况下忽略.gitignore是非常有用的。例如,当您有多个团队或一个大团队在同一个代码库上工作时。在这种情况下,您需要具有某些约定,其中一个约定是关于git repo中忽略的内容。它通常是忽略由IDE或OS创建的文件和目录,一些生成的日志等。

However, there is a force which is tending to introduce non-conventional changes to .gitignore file. The .gitignore file can be further changed by irresponsible person, by mistake, by a tool that is used, or in some other case.

但是,有一种力量倾向于对.gitignore文件引入非常规更改。 .gitignore文件可以由不负责任的人,错误地,使用的工具或在某些其他情况下进一步改变。

To have a counter force to this, we can do as followed:

为了对此有反作用力,我们可以这样做:

  1. The initial .gitignore should reflect convention in team(s),
  2. 最初的.gitignore应该反映团队中的惯例,

  3. After it is pushed, the .gitignore should be secured by adding .gitignore entry and push that change again.The .gitignore file is "sealed" in this way.
  4. 在推送之后,应该通过添加.gitignore条目来保护.gitignore并再次推送该更改。.gitignore文件以这种方式“密封”。

The "sealed" .gitignore file can be changed, just locally, without propagating that changers to other members of team(s). However, if a change is widely agreed throughout the whole team(s) than it is possible to "unseal" it, change it and than "seal" it again. That can't be done by mistake, only intentionally.

“密封的”.gitignore文件可以在本地更改,而不会将更改者传播给团队的其他成员。但是,如果在整个团队中广泛同意变更,而不是“开封”它,则更改它并再次“密封”它。这不可能是故意的,只是故意的。

Sadly, you cannot be 100% protected from the stupidity, but this way you have done everything you can to prevent stupid things to happen.

可悲的是,你不能100%保护免受愚蠢,但这样你就已经尽了一切力量来防止愚蠢的事情发生。

If you have relatively small team with very good professionals, than this wouldn't be important, but even those guys would appreciate to have one thing less to worry about.

如果你有一个相对较小的团队和非常优秀的专业人士,那么这并不重要,但即便是那些家伙也会欣赏有一点不用担心。

Using .git/info/exclude is cool when you cannot do anything about infrastructure settings, just covering your own a** not to make a mistake.

当你无法对基础设施设置做任何事情时,使用.git / info / exclude很酷,只是覆盖你自己的**而不是犯错误。

From a standing point of what is right and what is wrong I am voting for having .gitignore entry inside .gitignore file, giving everybody the freedom to do locally whatever they want, but not invading others.

从正确和错误的立场来看,我投票给.gitignore文件中的.gitignore条目,让每个人都可以*地在本地做任何他们想要的事情,但不会侵犯他人。

#19


I found that the best place to set up an ignore to the pesky .DS_Store files is in the .git/info/exclude file.

我发现设置忽略令人讨厌的.DS_Store文件的最佳位置是在.git / info / exclude文件中。

IntelliJ seems to do this automatically when you set up a git repository in it.

当您在其中设置git存储库时,IntelliJ似乎会自动执行此操作。

#1


The .gitignore file should be in your repository, so it should indeed be added and committed in, as git status suggests. It has to be a part of the repository tree, so that changes to it can be merged and so on.

.gitignore文件应该在您的存储库中,因此它确实应该被添加并提交,如git status所示。它必须是存储库树的一部分,因此可以合并对它的更改等。

So, add it to your repository, it should not be gitignored.

因此,将它添加到您的存储库,它不应该被gitignored。

If you really want you can add .gitignore to the .gitignore file if you don't want it to be committed. However, in that case it's probably better to add the ignores to .git/info/exclude, a special checkout-local file that works just like .gitignore but does not show up in "git status" since it's in the .git folder.

如果您真的想要,可以将.gitignore添加到.gitignore文件中,如果您不希望它被提交。但是,在这种情况下,最好将忽略添加到.git / info / exclude,这是一个特殊的checkout-local文件,其工作方式与.gitignore类似但不会显示在“git status”中,因为它位于.git文件夹中。

See also https://help.github.com/articles/ignoring-files

另请参见https://help.github.com/articles/ignoring-files

#2


If you want to store the list of ignored files outside of your Git tree, you can use the .git/info/exclude file. It is applied only to your checkout of the repo.

如果要将忽略文件列表存储在Git树之外,可以使用.git / info / exclude文件。它仅适用于您的结帐回收。

#3


You could actually put a line ".gitignore" into your ".gitignore" file. This would cause the ".gitignore" file to be ignored by git. I do not actually think this is a good idea. I think the ignore file should be version controlled and tracked. I'm just putting this out there for completeness.

您实际上可以在“.gitignore”文件中添加一行“.gitignore”。这将导致git忽略“.gitignore”文件。我实际上并不认为这是个好主意。我认为忽略文件应该受版本控制和跟踪。我只是为了完整而把它放在那里。

#4


You can also have a global user git .gitignore file that will apply automatically to all your repos. This is useful for IDE and editor files (e.g. swp and *~ files for Vim). Change directory locations to suite your OS

您还可以拥有一个全局用户git .gitignore文件,该文件将自动应用于您的所有回购。这对IDE和编辑器文件很有用(例如,Vim的swp和*〜文件)。更改目录位置以适应您的操作系统

  1. Add to your ~/.gitconfig file

    添加到〜/ .gitconfig文件

    [core]
    excludesfile = /home/username/.gitignore
    
  2. Create a ~/.gitignore file with file patterns to be ignored

    创建一个带有要忽略的文件模式的〜/ .gitignore文件

  3. Save your dot files in another repo so you have a backup (optional).

    将您的点文件保存在另一个仓库中,以便备份(可选)。

Any time you copy, init or clone a repo your global gitignore file will be used as well.

每次复制,初始化或克隆存储库时,也会使用全局gitignore文件。

#5


If someone has already added a .gitignore to your repo, but you want to make some changes to it and have those changes ignored do the following:

如果有人已经在您的仓库中添加了.gitignore,但您想对其进行一些更改并忽略这些更改,请执行以下操作:

git update-index --assume-unchanged .gitignore

git update-index --assume-unchanged .gitignore

Source.

#6


After you add the .gitignore file and commit it, it will no longer show up in the "untracked files" list.

添加.gitignore文件并提交后,它将不再显示在“未跟踪文件”列表中。

git add .gitignore
git commit -m "add .gitignore file"
git status

#7


Just incase someone else has the same pain we had. We wanted to exclude a file that had already been committed.

只是让别人有同样的痛苦。我们想要排除已经提交的文件。

This post was way more useful: working with .git/info/exclude too late

这篇文章更有用:使用.git / info / exclude太晚了

Specifically what you need to ignore a file is actually use the command git remove See git rm (http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

具体你需要忽略一个文件实际上是使用命令git remove参见git rm(http://www.kernel.org/pub/software/scm/git/docs/git-rm.html)

you test it by going

你去试试吧

git rm --dry-run *.log
(if you say wanted to exclude all the log files)

git rm --dry-run * .log(如果你想要排除所有日志文件)

this will output what would be excluded if you ran it.

这将输出运行它时将被排除的内容。

then

you run it by going

你去吧

git rm *.log
(or whatever filename path / expression you want to)

git rm * .log(或者你想要的任何文件名路径/表达式)

Then add a *.log line to your .gitignore file.

然后在.gitignore文件中添加* .log行。

#8


First of all, as many others already said, your .gitignore should be tracked by Git (and should therefore not be ignored). Let me explain why.

首先,正如许多其他人已经说过的那样,你的.gitignore应该由Git跟踪(因此不应该被忽略)。让我解释一下原因。

(TL;DR: commit the .gitignore file, and use a global .gitignore to ignore files that are created by your IDE or operating system)

(TL; DR:提交.gitignore文件,并使用全局.gitignore忽略由IDE或操作系统创建的文件)

Git is, as you probably already know, a distributed version control system. This means that it allows you to switch back and forth between different versions (even if development has diverged into different branches) and it also allows multiple developers to work on the same project.

正如您可能已经知道的那样,Git是一个分布式版本控制系统。这意味着它允许您在不同版本之间来回切换(即使开发已经分散到不同的分支),并且它还允许多个开发人员在同一个项目上工作。

Although tracking your .gitignore also has benefits when you switch between snapshots, the most important reason for committing it is that you'll want to share the file with other developers who are working on the same project. By committing the file into Git, other contributers will automatically get the .gitignore file when they clone the repository, so they won't have to worry about accidentally committing a file that shouldn't be committed (such as log files, cache directories, database credentials, etc.). And if at some point the project's .gitignore is updated, they can simply pull in those changes instead of having to edit the file manually.

虽然在快照之间切换时跟踪.gitignore也有好处,但提交它的最重要原因是您希望与正在处理同一项目的其他开发人员共享该文件。通过将文件提交到Git,其他贡献者将在克隆存储库时自动获取.gitignore文件,因此他们不必担心意外提交不应提交的文件(例如日志文件,缓存目录,数据库凭证等)。如果在某个时刻项目的.gitignore被更新,他们可以简单地提取这些更改,而不必手动编辑文件。

Of course, there will be some files and folders that you'll want to ignore, but that are specific for you, and don't apply to other developers. However, those should not be in the project's .gitignore. There are two other places where you can ignore files and folders:

当然,会有一些您想要忽略的文件和文件夹,但这些文件和文件夹特定于您,并不适用于其他开发人员。但是,那些不应该在项目的.gitignore中。您还可以在其他两个位置忽略文件和文件夹:

  • Files and folders that are created by your operating system or IDE should be placed in a global .gitignore. The benefit is that this .gitignore is applied to all repositories on your computer, so you don't have to repeat this for every repository. And it's not shared with other developers, since they might be using a different operating system and/or IDE.
  • 由操作系统或IDE创建的文件和文件夹应放在全局.gitignore中。好处是这个.gitignore应用于您计算机上的所有存储库,因此您不必为每个存储库重复此操作。并且它不与其他开发人员共享,因为他们可能使用不同的操作系统和/或IDE。

  • Files that don't belong in the project's .gitignore, nor in the global .gitignore, can be ignored using explicit repository excludes in your_project_directory/.git/info/exclude. This file will not be shared with other developers, and is specific for that single repository
  • 使用your_project_directory / .git / info / exclude中的显式存储库排除,可以忽略不属于项目的.gitignore和全局.gitignore的文件。此文件不会与其他开发人员共享,并且特定于该单个存储库

#9


The idea is to put files that are specific to your project into the .gitignore file and (as already mentioned) add it to the repository. For example .pyc and .o files, logs that the testsuite creates, some fixtures etc.

我们的想法是将特定于项目的文件放入.gitignore文件中(如前所述)将其添加到存储库中。例如.pyc和.o文件,测试套件创建的日志,一些固定装置等。

For files that your own setup creates but which will not necessarily appear for every user (like .swp files if you use vim, hidden ecplise directories and the like), you should use .git/info/exclude (as already mentioned).

对于您自己的设置创建但不一定为每个用户显示的文件(如.swp文件,如果您使用vim,隐藏的ecplise目录等),您应该使用.git / info / exclude(如前所述)。

#10


Of course the .gitignore file is showing up on the status, because it's untracked, and git sees it as a tasty new file to eat!

当然.gitignore文件显示在状态上,因为它没有跟踪,git认为它是一个可口的新文件!

Since .gitignore is an untracked file however, it is a candidate to be ignored by git when you put it in .gitignore!

因为.gitignore是一个未经跟踪的文件,但是当你把它放入.gitignore时它是被git忽略的候选者!

So, the answer is simple: just add the line:

所以,答案很简单:只需添加以下行:

.gitignore # Ignore the hand that feeds!

to your .gitignore file!

到您的.gitignore文件!

And, contrary to August's response, I should say that it's not that the .gitignore file should be in your repository. It just happens that it can be, which is often convenient. And it's probably true that this is the reason .gitignore was created as an alternative to .git/info/exclude, which doesn't have the option to be tracked by the repository. At any rate, how you use your .gitignore file is totally up to you.

而且,与August的回复相反,我应该说,并不是.gitignore文件应该在你的存储库中。它恰好发生,这通常很方便。而且这可能是因为这就是为什么.gitignore被创建为.git / info / exclude的替代品,它没有存储库可以跟踪的选项。无论如何,你如何使用.gitignore文件完全取决于你。

For reference, check out the gitignore(5) manpage on kernel.org.

有关参考,请查看kernel.org上的gitignore(5)联机帮助页。

#11


Watch out for the following "problem" Sometimes you want to add directories but no files within those directories. The simple solution is to create a .gitignore with the following content:

注意以下“问题”有时你想在这些目录中添加目录但没有文件。简单的解决方案是创建一个包含以下内容的.gitignore:

*

This seams to work fine until you realize that the directory was not added (as expected to your repository. The reason for that is that the .gitignore will also be ignored, and thereby the directory is empty. Thus, you should do something like this:

这个接缝工作正常,直到你意识到没有添加目录(正如预期的那样存储库。原因是.gitignore也将被忽略,因此目录是空的。因此,你应该做这样的事情:

*
!.gitignore

#12


This seems to only work for your current directory to get Git to ignore all files from the repository.

这似乎只适用于您当前的目录,以使Git忽略存储库中的所有文件。

update this file

更新此文件

.git/info/exclude 

with your wild card or filename

用你的外卡或文件名

*pyc
*swp
*~

#13


In my case, I want to exclude an existing file. Only modifying .gitignore not work. I followed these steps:

就我而言,我想要排除现有文件。只修改.gitignore不起作用。我按照以下步骤操作:

git rm --cached dirToFile/file.php
vim .gitignore
git commit -a

In this way, I cleaned from cache the file that I wanted to exclude and after I added it to .gitignore.

通过这种方式,我从缓存中清除了我想要排除的文件,并在将其添加到.gitignore之后。

#14


Navigate to the base directory of your git repo and execute the following command:

导航到git仓库的基本目录并执行以下命令:

echo '\\.*' >> .gitignore

All dot files will be ignored, including that pesky .DS_Store if you're on a mac.

所有点文件都将被忽略,包括那个讨厌的.DS_Store,如果你在mac上。

#15


If you've already checked in .gitignore and you want to ignore modifications to it, check out this answer:

如果您已经签入.gitignore并且想要忽略对它的修改,请查看以下答案:

Try using this command:

尝试使用此命令:

git update-index --assume-unchanged FILENAME_TO_IGNORE

To reverse it (if you ever want to commit changes to it), use:

要反转它(如果您想要对其进行更改),请使用:

git update-index --no-assume-unchanged

UPDATE:

Here's how to list 'assume unchanged' files under current directory:

以下是如何列出当前目录下的“假设未更改”文件:

git ls-files -v | grep -E "^[a-z]"

As the -v option will use lowercase letters for 'assume unchanged' files.

由于-v选项将使用小写字母表示“假定未更改”文件。

#16


It is quite possible that an end user wants to have Git ignore the ".gitignore" file simply because the IDE specific folders created by Eclipse are probably not the same as NetBeans or another IDE. So to keep the source code IDE antagonistic it makes life easy to have a custom git ignore that isn't shared with the entire team as individual developers might be using different IDE's.

最终用户很可能希望让Git忽略“.gitignore”文件,因为Eclipse创建的IDE特定文件夹可能与NetBeans或其他IDE不同。因此,为了保持源代码IDE的对抗性,它使生活变得容易,因为单个开发人员可能使用不同的IDE,因此无法与整个团队共享自定义git忽略。

#17


.gitignore is about ignoring other files. git is about files so this is about ignoring files. However as git works off files this file needs to be there as the mechanism to list the other file names.

.gitignore是关于忽略其他文件。 git是关于文件的,所以这是关于忽略文件。但是,当git处理文件时,此文件需要作为列出其他文件名的机制。

If it were called .the_list_of_ignored_files it might be a little more obvious.

如果它被称为.the_list_of_ignored_files,它可能会更加明显。

An analogy is a list of to-do items that you do NOT want to do. Unless you list them somewhere is some sort of 'to-do' list you won't know about them.

类比是您不想做的待办事项列表。除非你在某个地方列出某些“待办事项”列表,否则你将无法了解它们。

#18


I think that there are situations where ignoring the .gitignore is very useful. For instance, when you have multiple teams or a large team working on the same codebase. In that case, you need to have certain conventions, one of those convention is regarding what is ignored at the git repo. It is usually about ignoring files and directories created by IDE or OS, some generated logs, etc.

我认为有些情况下忽略.gitignore是非常有用的。例如,当您有多个团队或一个大团队在同一个代码库上工作时。在这种情况下,您需要具有某些约定,其中一个约定是关于git repo中忽略的内容。它通常是忽略由IDE或OS创建的文件和目录,一些生成的日志等。

However, there is a force which is tending to introduce non-conventional changes to .gitignore file. The .gitignore file can be further changed by irresponsible person, by mistake, by a tool that is used, or in some other case.

但是,有一种力量倾向于对.gitignore文件引入非常规更改。 .gitignore文件可以由不负责任的人,错误地,使用的工具或在某些其他情况下进一步改变。

To have a counter force to this, we can do as followed:

为了对此有反作用力,我们可以这样做:

  1. The initial .gitignore should reflect convention in team(s),
  2. 最初的.gitignore应该反映团队中的惯例,

  3. After it is pushed, the .gitignore should be secured by adding .gitignore entry and push that change again.The .gitignore file is "sealed" in this way.
  4. 在推送之后,应该通过添加.gitignore条目来保护.gitignore并再次推送该更改。.gitignore文件以这种方式“密封”。

The "sealed" .gitignore file can be changed, just locally, without propagating that changers to other members of team(s). However, if a change is widely agreed throughout the whole team(s) than it is possible to "unseal" it, change it and than "seal" it again. That can't be done by mistake, only intentionally.

“密封的”.gitignore文件可以在本地更改,而不会将更改者传播给团队的其他成员。但是,如果在整个团队中广泛同意变更,而不是“开封”它,则更改它并再次“密封”它。这不可能是故意的,只是故意的。

Sadly, you cannot be 100% protected from the stupidity, but this way you have done everything you can to prevent stupid things to happen.

可悲的是,你不能100%保护免受愚蠢,但这样你就已经尽了一切力量来防止愚蠢的事情发生。

If you have relatively small team with very good professionals, than this wouldn't be important, but even those guys would appreciate to have one thing less to worry about.

如果你有一个相对较小的团队和非常优秀的专业人士,那么这并不重要,但即便是那些家伙也会欣赏有一点不用担心。

Using .git/info/exclude is cool when you cannot do anything about infrastructure settings, just covering your own a** not to make a mistake.

当你无法对基础设施设置做任何事情时,使用.git / info / exclude很酷,只是覆盖你自己的**而不是犯错误。

From a standing point of what is right and what is wrong I am voting for having .gitignore entry inside .gitignore file, giving everybody the freedom to do locally whatever they want, but not invading others.

从正确和错误的立场来看,我投票给.gitignore文件中的.gitignore条目,让每个人都可以*地在本地做任何他们想要的事情,但不会侵犯他人。

#19


I found that the best place to set up an ignore to the pesky .DS_Store files is in the .git/info/exclude file.

我发现设置忽略令人讨厌的.DS_Store文件的最佳位置是在.git / info / exclude文件中。

IntelliJ seems to do this automatically when you set up a git repository in it.

当您在其中设置git存储库时,IntelliJ似乎会自动执行此操作。