git 将 .gitignore 添加到 gitignore

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10176875/
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-10 13:28:37  来源:igfitidea点击:

Add .gitignore to gitignore

gitgitignore

提问by Lars Nystr?m

Is it possible to add the .gitignorefile to .gitignoreitself?

是否可以将.gitignore文件添加到.gitignore自身?

.gitignore

Doesn't work though

虽然不起作用

I don't want to see it in edited files

我不想在编辑过的文件中看到它

回答by Lars Nystr?m

The .gitignorefile's purpose is to prevent everyone who collaborates on a project from accidentally commiting some common files in a project, such as generated cache files. Therefore you should not ignore .gitignore, since it's supposed to be included in the repository.

.gitignore文件的目的是防止在项目上协作的每个人意外提交项目中的一些常见文件,例如生成的缓存文件。因此,您不应忽略.gitignore,因为它应该包含在存储库中。

If you want to ignore files in just one repository but want to avoid committing the ignore list (for example for personal files) you can add them to .git/info/excludein that repository.

如果您只想忽略一个存储库中的文件,但又不想提交忽略列表(例如个人文件),您可以将它们添加到.git/info/exclude该存储库中。

If you want to ignore certain files on every repository on your machine you can create the file ~/.gitignore_globaland then run

如果您想忽略机器上每个存储库上的某些文件,您可以创建该文件~/.gitignore_global然后运行

git config --global core.excludesfile ~/.gitignore_global

回答by Mark E. Haase

A .gitignore can ignore itself if it's never been checked in:

如果 .gitignore从未被签入,它可以忽略自身

mhaase@ubuntu:~$ git --version
git version 1.7.9.5
mhaase@ubuntu:~$ git init temp
Initialized empty Git repository in /home/mhaase/temp/.git/
mhaase@ubuntu:~$ cd temp
mhaase@ubuntu:~/temp$ touch .gitignore foo bar baz bat
mhaase@ubuntu:~/temp$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       .gitignore
#       bar
#       bat
#       baz
#       foo
mhaase@ubuntu:~/temp$ echo "foo" >> .gitignore
mhaase@ubuntu:~/temp$ echo ".gitignore" >> .gitignore
mhaase@ubuntu:~/temp$ git status
# On branch master
#
# Initial commit
#
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       bar
#       bat
#       baz
nothing added to commit but untracked files present (use "git add" to track)

If you check in .gitignore (before you tell it to ignore itself), then it will always show up in git status, even if you later modify it to ignore itself.

如果您签入 .gitignore(在您告诉它忽略自身之前),那么它会始终显示在 git status 中,即使您稍后将其修改为忽略自身。

回答by Daenyth

There's not really a good reason to do this. If you want files ignored for your clone only, add them to .git/info/exclude, not .gitignore

这样做并没有什么好的理由。如果您只想为克隆忽略文件,请将它们添加到 .git/info/exclude,而不是 .gitignore

回答by Arnab Sanyal

After you enter .gitignorein your gitignore file, try the following,

输入.gitignoregitignore 文件后,请尝试以下操作,

git rm -r --cached .
git add --all
git commit -m "ignoring gitignore"
git push --set-upstream origin master

It should work although like already said, ignoring gitignore can be counter-productive if your repo is shared by multiple users.

它应该可以工作,尽管如前所述,如果您的存储库由多个用户共享,则忽略 gitignore 可能会适得其反。

回答by Ankit Singh

You can achieve this by writing this into your .gitignore file. The *will add everything to ignore list and then !.gitignorewill remove the .gitignore file from ignore list.

您可以通过将其写入 .gitignore 文件来实现此目的。该*会加重一切忽略列表,然后!.gitignore将删除忽略列表中的.gitignore文件。

*
!.gitignore

回答by CharlesB

Yes you can; you still see it in edited files because it is still tracked by git, and files tracked by git are always marked as modified even if they are in .gitignore. So simply untrackit.

是的你可以; 你仍然可以在编辑过的文件中看到它,因为它仍然被 git 跟踪,并且被 git 跟踪的文件总是被标记为已修改,即使它们在 .gitignore 中。所以简单地取消跟踪它。

But why not committing or resetting changes that you have on it? It's a much better way to remove it from status... Also be aware that any fresh clone of you repo will have to add its .gitignore, which can be annoying.

但是为什么不提交或重置您对它所做的更改呢?这是将其从状态中删除的更好方法...另外请注意,您的回购的任何新克隆都必须添加它的.gitignore,这可能很烦人。