git .gitignore 不起作用

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

.gitignore does not work

git

提问by Gnu Engineer

I've a .gitignorefile at the root of my repo. The .gitignorefile has the following pattern to exclude the compiled Python files and that is the only line in the file.

.gitignore在我的 repo 的根目录下有一个文件。该.gitignore文件具有以下模式以排除已编译的 Python 文件,这是文件中唯一的一行。

*.pyc

Now when i do the following at the root of the repo.

现在,当我在 repo 的根执行以下操作时。

git init
git add . 
git status 

It shows that it still tracks the .pycfile and tries to add it as new file. See output below.

它表明它仍然跟踪.pyc文件并尝试将其添加为新文件。请参阅下面的输出。

System info: Windows 7, cygwin

系统信息:Windows 7,cygwin

Note: This issue is CLEARLY not about the ignored file being already tracked. I also tried both DOS- and Unix-style line endings on the .gitignorefile.

注意:此问题显然与已被跟踪的忽略文件无关。我还在文件上尝试了 DOS 和 Unix 风格的行尾.gitignore

git statusgives:

git status给出:

# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#   new file:   .gitignore
#   new file:   feedapp/__init__.py
#   new file:   feedapp/appconfig.py
#   new file:   feedapp/appconfig.pyc

How do I troubleshoot this further?

我该如何进一步解决这个问题?

回答by freespace

.gitignoreonly applies to untrackedfiles. If you are tracking a .pycthen .gitignorewon't apply. Remove the .pycwith git rmand next time you do a git statusit (and any others) won't show up in the list of untracked file and nor will it be automatically added.

.gitignore仅适用于未跟踪的文件。如果您要跟踪.pyc,然后.gitignore将不适用。删除.pycwith git rm,下次您执行git status它(和任何其他)时,它不会显示在未跟踪文件列表中,也不会自动添加。



Otherwise if you need to ignore a file already under version control, update the index to ignore changes to files already under version control:

否则,如果您需要忽略已受版本控制的文件,请更新索引以忽略对已受版本控制的文件的更改:

git update-index --assume-unchanged <files>

For more information please see git-update-index(1) Manual Page, the related answer to .gitignore file not ignoringand the related answer to question (GIT: Ignoring Version-Controlled Files).

欲了解更多信息,请参阅的git更新索引(1)手册页中,相关的答案没有忽视的.gitignore文件相关答案的问题(GIT:忽略版本控制文件

回答by ikegami

man gitignore:

man gitignore

A gitignore file specifies intentionally **untracked files** that git should ignore. Note that all the gitignore files really concern only files that are not already tracked by git
gitignore 文件特意指定了 git 应该忽略的 **untracked files**。请注意,所有 gitignore 文件实际上只涉及 git 尚未跟踪的文件

git rm filewill stop track them. I can't find a way to remove all ignored files from the repo.

git rm file将停止跟踪他们。我找不到从存储库中删除所有被忽略文件的方法。



As you point out, the files don't appear to already exist in the repo. In that case, your git's behaviour matches neither the documentation or the behaviour of mine, and I can't help you.

正如您所指出的,这些文件似乎并不存在于 repo 中。在这种情况下,您git的行为与文档或我的行为都不匹配,我无法帮助您。

$ mkdir foo
$ cd foo
/home/ikegami/foo
$ mkdir feedapp
$ touch feedapp/__init__.py
$ touch feedapp/appconfig.py
$ touch feedapp/appconfig.pyc
$ echo '*.pyc' > .gitignore
$ git init
Initialized empty Git repository in /home/ikegami/foo/.git/
$ git add .
$ git status
# On branch master
#
# Initial commit
#
# Changes to be committed:
#   (use "git rm --cached <file>..." to unstage)
#
#       new file:   .gitignore
#       new file:   feedapp/__init__.py
#       new file:   feedapp/appconfig.py
#
$ 


Perhaps you did

也许你做了

git init
git add .
echo '*.pmc' >> .gitignore
git init
git add .

in which case you can fix the problem using

在这种情况下,您可以使用解决问题

git rm --cached -r .
git add .

回答by user1018882

I had the exact same problem. I found out that running "someText" > .gitignoremade a file with a weird encoding. I changed it to UTF-8, then all worked fine.

我有同样的问题。我发现运行生成"someText" > .gitignore了一个带有奇怪编码的文件。我将其更改为 UTF-8,然后一切正常。

回答by Houken

I have had exactly the same problem--even if I prepare a .gitignore file before the first time I add anything. And I found the problem was in the blank space in my ignore file. DO NOT add any white space in your ignore file and try again. I think everything will just work fine. Good luck

我遇到了完全相同的问题——即使我在第一次添加任何内容之前准备了一个 .gitignore 文件。我发现问题出在我的忽略文件中的空白处。不要在您的忽略文件中添加任何空格,然后重试。我认为一切都会正常进行。祝你好运

回答by Justin

Check the line endings on your .gitignore file. In an OS X repo, mine was using CR line endings. Everything worked again after switching over to LFs.

检查 .gitignore 文件的行尾。在 OS X 存储库中,我使用的是 CR 行结尾。切换到 LF 后,一切都恢复了。

perl -p -e 's/\r/\n/g'  < .gitignore > .gitignore-new
mv .gitignore-new .gitignore

Console jockeys will likely want to use sedinstead.

控制台骑师可能会想要使用sed

Background: I had copied my working copy from a Windows repository after a power failure.

背景:我在断电后从 Windows 存储库复制了我的工作副本。