git Gitignore 不工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25436312/
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 not working
提问by user3280133
My .gitignore
file isn't working for some reason, and no amount of Googling has been able to fix it. Here is what I have:
.gitignore
由于某种原因,我的文件无法正常工作,谷歌搜索也无法修复它。这是我所拥有的:
*.apk
*.ap_
*.dex
*.class
**/bin/
**/gen/
.gradle/
build/
local.properties
**/proguard/
*.log
It's in the directory master
, which is my git repo. I'm running Git 1.8.4.2 because I'm on a MacBook running OSX 10.8.6.
它在目录中master
,这是我的 git 仓库。我正在运行 Git 1.8.4.2,因为我在运行 OSX 10.8.6 的 MacBook 上。
回答by Ahmad
The files/folder in your version control will not just delete themselves just because you added them to the .gitignore
. They are already in the repository and you have to remove them. You can just do that with this:
版本控制中的文件/文件夹不会仅仅因为您将它们添加到.gitignore
. 它们已经在存储库中,您必须将其删除。你可以这样做:
(Remember to commit everything you've changed before you do this.)
(请记住在执行此操作之前提交您已更改的所有内容。)
git rm -rf --cached .
git add .
This removes all files from the repository and adds them back (this time respecting the rules in your .gitignore
).
这将从存储库中删除所有文件并将它们添加回来(这次遵守 中的规则.gitignore
)。
回答by Nicolas
To untrack a single file that has already been added/initialized to your repository, i.e., stop tracking the file but not delete it from your system use: git rm --cached filename
要取消跟踪已添加/初始化到存储库的单个文件,即停止跟踪文件但不从系统中删除它,请使用: git rm --cached filename
To untrack every file that is now in your .gitignore
:
要取消跟踪现在在您的 中的每个文件.gitignore
:
First commit any outstanding code changes, and then, run this command:
首先提交所有未完成的代码更改,然后运行以下命令:
git rm -r --cached .
This removes any changed files from the index(staging area), then just run:
这将从索引(暂存区)中删除任何更改的文件,然后运行:
git add .
Commit it:
提交它:
git commit -m ".gitignore is now working"
回答by Garrett Simpson
After going down a bit of a bit of a rabbit hole trying to follow the answers to this question (maybe because I had to do this in a visual studio project), I found the easier path was to
在尝试遵循这个问题的答案(可能是因为我必须在视觉工作室项目中这样做)之后,我发现了一个更简单的方法是
Cut and paste the file(s) I no longer want to track into a temporary location
Commit the "deletion" of those files
Commit a modification of the
.gitignore
to exclude the files I had temporarily movedMove the files back into the folder.
剪切并粘贴我不想再跟踪的文件到临时位置
提交这些文件的“删除”
提交修改
.gitignore
以排除我临时移动的文件将文件移回文件夹。
I found this to be the most straight forward way to go about it (at least in a visual studio, or I would assume other IDE heave based environment like Android Studio), without accidentally shooting myself in the foot with a pretty pervasive git rm -rf --cached .
, after which the visual studio project I was working on didn't load.
我发现这是最直接的方法(至少在视觉工作室中,或者我会假设其他基于 IDE 的环境,如 Android Studio),而不会意外地用非常普遍的git rm -rf --cached .
.我正在处理的 Visual Studio 项目没有加载。
回答by S Saad
In my case it was a blank space at the beginning of the filewhich showed up clearly when I opened the file in Notepad, wasn't obvious in Visual Studio Code.
在我的情况下,它是文件开头的空白区域,当我在记事本中打开文件时,它清楚地显示出来,在 Visual Studio Code 中并不明显。
回答by DeezCashews
I used something to generate common .gitignore
for me and I ran into this. After reading @Ozesh answer I opened in VS Code because it has a nice indicator at bottom right showing type of line endings. It was LF so I converted to CRLF as suggested but no dice.
我用了一些东西来.gitignore
为我生成共同点,我遇到了这个。阅读@Ozesh 答案后,我在 VS Code 中打开,因为它在右下角有一个很好的指示器,显示行尾类型。这是LF,所以我按照建议转换为CRLF,但没有骰子。
Then I looked next to the line endings and noticed it was saved using UTF16. So I resaved using UTF8 encoding an voila, it worked. I didn't think the CRLF mattered so I changed it back to LF to be sure and it still worked.
然后我查看了行尾,注意到它是使用 UTF16 保存的。所以我使用 UTF8 编码重新保存,瞧,它起作用了。我认为 CRLF 并不重要,所以我将它改回 LF 以确保它仍然有效。
Of course this wasn't OPs issue since he had already committed the files so they were already indexed, but thought I'd share in case someone else stumbles across this.
当然,这不是 OP 问题,因为他已经提交了文件,因此它们已经被编入索引,但我想我会分享以防其他人偶然发现这一点。
TLDR; If you haven't already committed the files and .gitignore still isn't being respected then check file encoding and, make sure its UTF8 and if that doesn't work then maybe try messing with line endings.
TLDR; 如果您还没有提交文件并且 .gitignore 仍然没有得到尊重,那么检查文件编码,并确保它的 UTF8,如果这不起作用,那么也许尝试弄乱行尾。
回答by 026
In my case whitespaces at the end of the lines of .gitignore was the cause. So watch out for whitespaces in the .gitignore!
在我的情况下,.gitignore 行末尾的空格是原因。所以要注意 .gitignore 中的空格!
回答by Ozesh
I solved my problem doing the following:
我通过以下方式解决了我的问题:
First of all, I am a windows user, but i have faced similar issue. So, I am posting my solution here.
首先,我是 Windows 用户,但我也遇到过类似的问题。所以,我在这里发布我的解决方案。
There is one simple reason why sometimes the .gitignore doesn`t work like it is supposed to. It is due to the EOL conversion behavior.
有时 .gitignore 不能像预期的那样工作有一个简单的原因。这是由于 EOL 转换行为。
Here is a quick fix for that
这是一个快速解决方案
Edit > EOL Conversion > Windows Format > Save
编辑 > EOL 转换 > Windows 格式 > 保存
You can blame your text editor settingsfor that.
您可以为此归咎于您的文本编辑器设置。
For example:
例如:
As i am a windows developer, I typically use Notepad++for editing my text unlike Vim users.
由于我是一名 Windows 开发人员,因此与 Vim 用户不同,我通常使用Notepad++来编辑我的文本。
So what happens is, when i open my .gitignore file using Notepad++, it looks something like this:
所以会发生什么,当我使用 Notepad++ 打开我的 .gitignore 文件时,它看起来像这样:
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# See https://help.github.com/ignore-files/ for more about ignoring files.
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
*.dll
*.force
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
If i open the same file using the default Notepad, this is what i get
如果我使用默认记事本打开同一个文件,这就是我得到的
## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore # See https://help.github.com/ignore-files/ for more about ignoring files. # User-specific files *.suo *.user *.userosscache
So, you might have already guessed by looking at the output. Everything in the .gitignore has become a one liner, and since there is a ## in the start, it acts as if everything is commented.
因此,通过查看输出,您可能已经猜到了。.gitignore 中的所有内容都变成了一个单行,并且由于开头有一个 ##,它就像所有内容都被注释了一样。
The way to fix this is simple: Just open your .gitignore file with Notepad++ , then do the following
解决这个问题的方法很简单:只需使用 Notepad++ 打开您的 .gitignore 文件,然后执行以下操作
Edit > EOL Conversion > Windows Format > Save
编辑 > EOL 转换 > Windows 格式 > 保存
The next time you open the same file with the windows default notepad, everything should be properly formatted. Try it and see if this works for you.
下次使用 Windows 默认记事本打开同一个文件时,所有内容都应正确格式化。试试看,看看这是否适合你。
回答by Ben Pritchard
Also, comments have to be on their own line. They can't be put after an entry. So this won't work:
此外,评论必须在他们自己的行上。它们不能放在条目之后。所以这行不通:
/node_modules # DON'T COMMENT HERE (since nullifies entire line)
But this will work:
但这会起作用:
# fine to comment here
/node_modules
回答by Daniel Katz
Adding my bit as this is a popular question.
添加我的一点,因为这是一个受欢迎的问题。
I couldn't place .history directory inside .gitignore because no matter what combo I tried, it just didn't work. Windows keeps generating new files upon every save and I don't want to see these at all.
我无法将 .history 目录放在 .gitignore 中,因为无论我尝试过什么组合,它都不起作用。Windows 每次保存时都会不断生成新文件,我根本不想看到这些。
But then I realized, this is just my personal development environment on my machine. Things like .history or .vscode are specific for me so it would be weird if everyone included their own .gitignore entries based on what IDE or OS they are using.
但后来我意识到,这只是我机器上的个人开发环境。.history 或 .vscode 之类的东西对我来说是特定的,所以如果每个人都根据他们使用的 IDE 或操作系统包含自己的 .gitignore 条目会很奇怪。
So this worked for me, just append ".history" to .git/info/exclude
所以这对我有用,只需将“.history”附加到 .git/info/exclude
echo ".history" >> .git/info/exclude
回答by Doci
For iOS Users, ignoring files in GitHub through .gitignore is explained here: https://stackoverflow.com/a/57828954/7987502
对于 iOS 用户,这里解释了通过 .gitignore 忽略 GitHub 中的文件:https: //stackoverflow.com/a/57828954/7987502