.gitignore 被 Git 忽略
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11451535/
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
.gitignore is ignored by Git
提问by Matt Parkins
My .gitignore
file seems to be being ignored by Git - could the .gitignore
file be corrupt? Which file format, locale or culture does Git expect?
我的.gitignore
文件似乎被 Git 忽略了 -.gitignore
文件是否已损坏?Git 期望哪种文件格式、语言环境或文化?
My .gitignore
:
我的.gitignore
:
# This is a comment
debug.log
nbproject/
Output from git status
:
输出git status
:
# On branch master
# Your branch is ahead of 'origin/master' by 1 commit.
#
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# debug.log
# nbproject/
nothing added to commit but untracked files present (use "git add" to track)
I would like debug.log
and nbproject/
not to appear in the untracked files list.
我希望debug.log
并且nbproject/
不出现在未跟踪文件列表中。
Where should I start looking to fix this?
我应该从哪里开始寻找解决这个问题?
采纳答案by Matt Parkins
Fixed. OK, I created the .gitignore file in Notepad on Windows and it wasn't working. When I viewed the .gitignore file on Linux it looked like organised gibberish - perhaps Notepad had written out Unicode rather than ASCII or whatever 8-bit is.
固定的。好的,我在 Windows 上的记事本中创建了 .gitignore 文件,但它不起作用。当我在 Linux 上查看 .gitignore 文件时,它看起来像是有组织的胡言乱语 - 也许记事本写出了 Unicode 而不是 ASCII 或任何 8 位。
So I rewrote the file on my Linux box, and when I pulled it back into Windows it works fine! Hurrah!
所以我在我的 Linux 机器上重写了这个文件,当我把它拉回 Windows 时它工作正常!欢呼!
回答by Alin Huruba
Even if you haven't tracked the files so far, Git seems to be able to "know" about them even after you add them to .gitignore
.
即使到目前为止您还没有跟踪这些文件,Git 似乎即使在您将它们添加到.gitignore
.
WARNING:First commit your current changes, or you will lose them.
警告:首先提交您当前的更改,否则您将丢失它们。
Then run the following commands from the top folder of your Git repository:
然后从 Git 存储库的顶部文件夹运行以下命令:
git rm -r --cached .
git add .
git commit -m "fixed untracked files"
回答by ifeegoo
If it seems like Git isn't noticing the changes you made to your .gitignore
file, you might want to check the following points:
如果 Git 似乎没有注意到您对.gitignore
文件所做的更改,您可能需要检查以下几点:
- There might be a global
.gitignore
file that might interfere with your local one When you add something into a .gitignore file, try this:
git add [uncommitted changes you want to keep] && git commit git rm -r --cached . git add . git commit -m "fixed untracked files"
If you remove something from a .gitignore file, and the above steps maybe don't work,if you found the above steps are not working, try this:
git add -f [files you want to track again] git commit -m "Refresh removing files from .gitignore file." // For example, if you want the .java type file to be tracked again, // The command should be: // git add -f *.java
- 可能有一个全局
.gitignore
文件可能会干扰您的本地文件 当你在 .gitignore 文件中添加一些东西时,试试这个:
git add [uncommitted changes you want to keep] && git commit git rm -r --cached . git add . git commit -m "fixed untracked files"
如果您从 .gitignore 文件中删除某些内容,并且上述步骤可能不起作用,如果您发现上述步骤不起作用,请尝试以下操作:
git add -f [files you want to track again] git commit -m "Refresh removing files from .gitignore file." // For example, if you want the .java type file to be tracked again, // The command should be: // git add -f *.java
回答by H A?d?μ
Without adding another commit to your project, one line will be enough to make .gitignore
work as it is supposed to:
无需向您的项目添加另一个提交,一行就足以使.gitignore
工作正常:
git rm -r --cached debug.log nbproject
This will remove them from the repository, but still keep them physically. In plain English, it deletes any history of changes related to them, and also will not track their change in any future commit. You may find a better explanation here.
这将从存储库中删除它们,但仍然保留它们的物理。简单来说,它会删除与它们相关的任何更改历史记录,并且也不会在任何未来的提交中跟踪它们的更改。您可能会在此处找到更好的解释。
回答by Rawa
Another cause of this issue is blank spaces or tabs before the statement:
此问题的另一个原因是语句前的空格或制表符:
Example:
例子:
# Be aware of the following:
notWorkingIgnore.*
workingIgnore.*
And as pointed out by the comment below a trailing space can be an issue as well:
正如下面的评论所指出的,尾随空格也可能是一个问题:
# Be aware of the following:
notWorkingIgnore.* #<-Space
workingIgnore.*#<-Nospace
回答by craig
I noticed that the encoding of the .gitignore
was having an effect--if the file was Unicode, it was ignored, if it was ASCII, it wasn't.
我注意到 的编码.gitignore
有影响——如果文件是 Unicode,它被忽略,如果它是 ASCII,它不是。
Process:
过程:
- Verify status:
PS> git status
- Create a function to Get-FileEncoding
- Test
.gitignore
's encoding:PS> Get-FileEncoding .gitignore
- Change the encodingto ASCII:
PS> Set-Content .gitignore -Encoding Ascii -Value (Get-Content .gitignore)
- Confirm:
PS> git status
回答by Steven Stark
As with the other solutions, commit first and be aware that you willlose any un-committed changes.
与其他解决方案一样,首先提交并注意您将丢失任何未提交的更改。
I had better results with this:
我有更好的结果:
git rm -r --cached .
git reset HEAD --hard
git status
Note that the status shouldn't have any modified files now.
请注意,状态现在不应该有任何修改过的文件。
回答by Edesa
In my case, it's because the files already exist in the repository and I'm trying to ignore it.
就我而言,这是因为文件已存在于存储库中,而我试图忽略它。
These are the things I did to fix the issue:
这些是我为解决问题所做的事情:
- Copy the files to a temporary folder
- Remove them from my project folder.
- Commit the changes which remove those files from the repository
- Re-added those files to my project folder
- 将文件复制到临时文件夹
- 从我的项目文件夹中删除它们。
- 提交从存储库中删除这些文件的更改
- 将这些文件重新添加到我的项目文件夹中
By then, any changes I made on those files were ignored.
到那时,我对这些文件所做的任何更改都将被忽略。
I think you can't ignore files that already exist on the repository.
我认为您不能忽略存储库中已经存在的文件。
回答by CESCO
All the answers here are actually workarounds. You need to create the .gitignore file beforeyou run git init
. Otherwise git
will never know you need to ignore those files, because they have been tracked already.
这里的所有答案实际上都是解决方法。您需要在运行之前创建 .gitignore 文件git init
。否则git
永远不会知道您需要忽略这些文件,因为它们已经被跟踪了。
echo .idea/ >> .gitignore
git init
If you develop on a daily basis, I advise you to add your habitual ignored files to your ~/.gitignore_global
file. That way, git
will already know which files you (meaning "your user", since it's a file in your home directory) usually ignore.
如果你是日常开发,我建议你把你习惯性忽略的文件添加到你的~/.gitignore_global
文件中。这样,您git
就已经知道您(意思是“您的用户”,因为它是您的主目录中的一个文件)通常会忽略哪些文件。
回答by Artem Zaytsev
Also check out the directorywhere you put .gitignore
.
还要检查您放置的目录.gitignore
。
It should be in the root of your project:
它应该在您项目的根目录中:
./myproject/.gitignore
Not in
不在
./myproject/.git/.gitignore