将.gitignore添加到gitignore

111 git gitignore

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

.gitignore
Run Code Online (Sandbox Code Playgroud)

虽然不起作用

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

Lar*_*röm 139

.gitignore文件旨在防止在项目上进行协作的每个人在项目中包含一些常见文件,例如生成的缓存文件.因此你不应该忽视.gitignore,因为它的目的是被包含在回购中.

如果您只想忽略一个仓库中的文件但没有提交忽略列表,则可以将它们添加到.git/info/exclude该仓库中.

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

git config --global core.excludesfile ~/.gitignore_global
Run Code Online (Sandbox Code Playgroud)

  • 虽然这是一个详细的答案,但它并没有回答问题。“不应该”并不意味着你不能。在我的特定情况下,我有一个包含 .gitignore 文件的符号链接,现在当我想提交时,git 会尝试包含该 .gitignore 文件。在我看来,这似乎是一个完美的用例。 (4认同)

Mar*_*ase 29

如果从未检入过, .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)
Run Code Online (Sandbox Code Playgroud)

如果您签入.gitignore(在您告诉它忽略自己之前),那么它将始终以git状态显示,即使您稍后修改它以忽略它自己.

  • 没错,这就是我的情况。我想要一个本地的 `.gitignore` 来处理 `git-svn`,没有人应该看到它。所以我只是将它添加到它本身,它就起作用了。但后来我在谷歌上搜索,看看是否有些紧张的人会向其他人解释这有多“坏”。这个答案应该被接受,然后我也会赞成`.git/info/exclude` 建议。所以它只需要不被追踪......这并不奇怪,至少如果你曾经被“颠覆”(svn)。 (2认同)
  • 我先备份了 `.gitignore` 文件。然后我使用 `git rm .gitignore` 并提交该更改。最后,使用“.gitignore”的规则再次添加并提交“.gitignore”文件。有用! (2认同)
  • 要解决这个问题,您可以运行`git rm --cached .gitignore`。 (2认同)

Dae*_*yth 18

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


Arn*_*yal 12

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

git rm -r --cached .
git add --all
git commit -m "ignoring gitignore"
git push --set-upstream origin master
Run Code Online (Sandbox Code Playgroud)

它应该可以工作,尽管就像已经说过的那样,如果您的存储库由多个用户共享,则忽略 gitignore 可能会适得其反。