git 如何阻止 .gitignore 出现在未跟踪文件列表中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/767147/
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
How can I stop .gitignore from appearing in the list of untracked files?
提问by Jacques René Mesrine
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
, .gitignorefile appears in the list of untracked files. Why is that?
现在,当我输入git status
,的.gitignore文件出现在未跟踪文件列表。这是为什么?
采纳答案by August Lilleaas
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.
因此,将其添加到您的存储库中,不应忽略它。
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
文件中。但是,在这种情况下,最好将 ignores 添加到.git/info/exclude
.gitignore 一个特殊的检出本地文件,它的工作方式与 .gitignore 类似,但不会出现在“git status”中,因为它位于.git
文件夹中。
回答by Pawe? Hajdan
If you want to store the list of ignored files outside of your Git tree, you can use the .git/info/excludefile. It is applied only to your checkout of the repo.
如果您想在 Git 树之外存储被忽略文件的列表,您可以使用.git/info/exclude文件。它仅适用于您对 repo 的结帐。
回答by 1800 INFORMATION
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
文件中添加一行。这将导致.gitignore
文件被 git 忽略。我实际上并不认为这是一个好主意。我认为应该对忽略文件进行版本控制和跟踪。我只是把它放在那里是为了完整性。
回答by Alec the Geek
You can also have a global user git .gitignore
file that will apply automatically to allyour repos. This is useful for IDE and editor files (e.g. swp
and *~
files for Vim). Change directory locations to suit your OS.
您还可以拥有一个全局用户 git.gitignore
文件,该文件将自动应用于您的所有存储库。这是IDE和编辑文件(例如有用swp
和*~
文件VIM)。更改目录位置以适合您的操作系统。
Add to your
~/.gitconfig
file:[core] excludesfile = /home/username/.gitignore
Create a
~/.gitignore
file with file patterns to be ignored.Save your dot files in another repo so you have a backup (optional).
添加到您的
~/.gitconfig
文件:[core] excludesfile = /home/username/.gitignore
创建一个
~/.gitignore
包含要忽略的文件模式的文件。将您的点文件保存在另一个 repo 中,以便您有一个备份(可选)。
Any time you copy, init or clone a repo, your global gitignore file will be used as well.
任何时候你复制、初始化或克隆一个 repo,你的全局 gitignore 文件也将被使用。
回答by Leif Gruenwoldt
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:
如果有人已经将 a 添加.gitignore
到您的存储库,但您想对其进行一些更改并忽略这些更改,请执行以下操作:
git update-index --assume-unchanged .gitignore
git update-index --assume-unchanged .gitignore
来源。
回答by Greg Hewgill
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
回答by Evolve
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 beexcluded 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.
然后*.log
在您的.gitignore
文件中添加一行。
回答by chase
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 shouldbe in your repository. It just happens that it canbe, 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 的替代品的原因,因为 .git/info/exclude 没有被存储库跟踪的选项。无论如何,您如何使用 .gitignore 文件完全取决于您。
For reference, check out the gitignore(5) manpageon kernel.org.
作为参考,请查看kernel.org 上的gitignore(5) 联机帮助页。
回答by bayer
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
(如前所述)。
回答by Nic Wortel
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. - Files that don't belong in the project's
.gitignore
, nor in the global.gitignore
, can be ignored using explicit repository excludes inyour_project_directory/.git/info/exclude
. This file will not be shared with other developers, and is specific for that single repository
- 由您的操作系统或 IDE 创建的文件和文件夹应放置在全局
.gitignore
. 好处是这.gitignore
适用于您计算机上的所有存储库,因此您不必为每个存储库重复此操作。它不会与其他开发人员共享,因为他们可能使用不同的操作系统和/或 IDE。 - 不属于项目的
.gitignore
,也不属于全局 的文件.gitignore
可以使用显式存储库排除 inyour_project_directory/.git/info/exclude
忽略。此文件不会与其他开发人员共享,并且特定于该单个存储库