为什么 Git 不忽略我指定的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3833561/
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
Why doesn't Git ignore my specified file?
提问by Nick.h
I added the following line to .gitignore
:
我将以下行添加到.gitignore
:
sites/default/settings.php
but when I type git status
it shows the file as unstaged file.
但是当我输入时,git status
它会将文件显示为未暂存的文件。
What's the problem? All other patterns work well.
有什么问题?所有其他模式都运行良好。
回答by poke
Make sure that your .gitignore
is in the root of the working directory, and in that directory run git status
and copythe path to the file from the status output and paste it into the .gitignore
.
确保您.gitignore
位于工作目录的根目录中,并在该目录中运行git status
并从状态输出复制文件的路径并将其粘贴到.gitignore
.
If that doesn't work, then it's likely that your file is already tracked by Git. You can confirm this through the output of git status
. If the file is not listed in the “Untracked files”section, then it is already tracked by Git and it will ignore the rule from the .gitignore
file.
如果这不起作用,则很可能您的文件已被 Git 跟踪。您可以通过 的输出确认这一点git status
。如果该文件未在“未跟踪的文件”部分中列出,则它已被 Git 跟踪,它将忽略.gitignore
文件中的规则。
The reason to ignore files in Git is so that they won't be added to the repository. If you previously added a file you want to be ignored, then it will be tracked by Git and the ignore rules matching it will be skipped. Git does this since the file is already part of the repository.
在 Git 中忽略文件的原因是它们不会被添加到存储库中。如果您之前添加了要忽略的文件,则 Git 将跟踪该文件并跳过匹配它的忽略规则。Git 这样做是因为该文件已经是存储库的一部分。
In order to actually ignore the file, you have to untrack it and remove it from the repository. You can do that by using git rm --cached sites/default/settings.php
. This removes the file from the repository without physically deleting the file (that's what the --cached
does). After committing that change, the file will be removed from the repository, and ignoring it should work properly.
为了实际忽略该文件,您必须取消跟踪它并将其从存储库中删除。您可以使用git rm --cached sites/default/settings.php
. 这会从存储库中删除文件而不实际删除文件(这就是这样--cached
做的)。提交更改后,该文件将从存储库中删除,忽略它应该可以正常工作。
回答by Mescalito
I run into this, it's an old question, but I want that file to be tracked but to not track it on certain working copies, to do that you can run
我遇到了这个,这是一个老问题,但我希望跟踪该文件,但不要在某些工作副本上跟踪它,为此您可以运行
git update-index --assume-unchanged sites/default/settings.php
回答by jonescb
.gitignore will only ignore files that you haven't already added to your repository.
.gitignore 只会忽略您尚未添加到存储库中的文件。
If you did a git add .
, and the file got added to the index, .gitignore won't help you. You'll need to do git rm sites/default/settings.php
to remove it, and then it will be ignored.
如果你做了一个git add .
,并且文件被添加到索引中, .gitignore 不会帮助你。您需要git rm sites/default/settings.php
删除它,然后它将被忽略。
回答by LiQiang
Please use this command
请使用此命令
git rm -rf --cached .
git add .
Sometimes .gitignore files don't work even though they're correct. The reason Git ignores files is that they are not added to the repository. If you added a file that you want to ignore before, it will be tracked by Git, and any skipping matching rules will be skipped. Git does this because the file is already part of the repository.
有时 .gitignore 文件即使正确也不起作用。Git 忽略文件的原因是它们没有被添加到存储库中。如果你之前添加了一个你想忽略的文件,它会被 Git 跟踪,任何跳过的匹配规则都会被跳过。Git 这样做是因为该文件已经是存储库的一部分。
回答by Floris Devreese
I had the same problem. Files defined in .gitingore
where listed as untracked files when running git status
.
我有同样的问题。在.gitingore
where 中定义的文件在运行时列为未跟踪文件git status
。
The reason was that the .gitignore
file was saved in UTF-16LE
encoding, and not in UTF8
encoding.
原因是.gitignore
文件是以UTF-16LE
编码方式保存的,而不是以UTF8
编码方式保存的。
After changing the encoding of the .gitignore
file to UTF8
it worked for me.
将.gitignore
文件的编码更改UTF8
为对我有用后。
回答by Alper Ebicoglu
What I did it to ignore the settings.phpfile successfully:
我做了什么以成功忽略settings.php文件:
- git rm --cached sites/default/settings.php
- commit (up to here didn't work)
- manually deleted sites/default/settings.php (this did the trick)
- git add .
- commit (ignored successfully)
- git rm --cached 站点/默认/settings.php
- 提交(到这里没有工作)
- 手动删除的网站/默认/settings.php(这成功了)
- git 添加。
- 提交(成功忽略)
I think if there's the committed file on Git then ignore doesn't work as expected. Just delete the file and commit. Afterwards it'll ignore.
我认为如果 Git 上有提交的文件,那么 ignore 不会按预期工作。只需删除文件并提交即可。之后它会忽略。
回答by J.Leupp
There are instances e.g. Application Configuration files, which I want tracked in git (so .gitignore will not work), but that I need to change for local settings. I do not want git to manage these files or show them as modified. To do this I use skip-worktree:
有一些实例,例如应用程序配置文件,我想在 git 中对其进行跟踪(因此 .gitignore 将不起作用),但我需要更改本地设置。我不希望 git 管理这些文件或将它们显示为已修改。为此,我使用跳过工作树:
git update-index --skip-worktree path/to/file
You can confirm files are skipped by listing files and checking for lines starting with S for skipped
您可以通过列出文件并检查以 S 开头的行来确认文件被跳过
git ls-files -v | grep ^S
If in the future you want to have git manage the file locally again simply run:
如果以后你想让 git 再次在本地管理文件,只需运行:
git update-index --no-skip-worktree path/to/file
Mescalito above had a great answer, that led me down the right track but
上面的麦斯卡力陀有一个很好的答案,这使我走上了正确的道路,但是
git update-index --assume-unchanged file/to/ignore.php
git update-index --assume-unchanged file/to/ignore.php
Has a contract with git that in which : the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index.
与 git 有一个合同,其中:用户承诺不更改文件并允许 Git 假设工作树文件与索引中记录的内容相匹配。
However, I change the content of the files, so in my case --skip-worktree is the better option.
但是,我更改了文件的内容,因此在我的情况下 --skip-worktree 是更好的选择。
Toshiharu Nishina's website provided an excellent explanation of skip-worktree vs assume-unchanged: Ignore files already managed with Git locally
Toshiharu Nishina 的网站提供了关于跳过工作树与假设不变的极好解释:忽略已在本地使用 Git 管理的文件
回答by Oleg Zarevennyi
Another possible reason– a few instances of git clients running at the same time. For example "git shell" + "GitHub Desktop", etc.
另一个可能的原因–几个 git 客户端实例同时运行。例如“git shell”+“GitHub 桌面”等。
This happened to me, I was using "GitHub Desktop" as the main client and it was ignoring some new .gitignore settings: commit after commit:
这发生在我身上,我使用“GitHub Desktop”作为主要客户端,它忽略了一些新的 .gitignore 设置:提交后提交:
- You commit something.
- Next, commit: it ignores .gitignore settings. Commit includes lots of temp files mentioned in the .gitignore.
- Clear git cache; check whether .gitignore is UTF8; remove files -> commit -> move files back; skip 1 commit – nothing helped.
- 你犯了什么。
- 接下来,提交:它忽略 .gitignore 设置。Commit 包含许多在 .gitignore 中提到的临时文件。
- 清除git缓存;检查 .gitignore 是否为 UTF8;删除文件 -> 提交 -> 将文件移回;跳过 1 次提交——没有任何帮助。
Reason: the Visual Studio Code editor was running in the background with the same opened repository. VS Code has built-in git control, and this makes some conflicts.
原因:Visual Studio Code 编辑器在后台运行,并使用相同的打开的存储库。VS Code 内置了 git 控件,这就产生了一些冲突。
Solution: double-check multiple, hidden git clients and use only one git client at once, especially while clearing git cache.
解决方案:仔细检查多个隐藏的 git 客户端,一次只使用一个 git 客户端,尤其是在清除 git 缓存时。
回答by ZackOfAllTrades
Make sure the .gitignore does not have a extension!! It can't be .gitignore.txt, in windows just name the file .gitignore. and it will work.
确保 .gitignore 没有扩展名!!它不能是 .gitignore.txt,在 Windows 中只需将文件命名为 .gitignore。它会起作用。
回答by Alasdair Mclean
Just in case anyone in the future has the same problem that I did:
以防万一将来有人遇到和我一样的问题:
If you use the
如果您使用
*
!/**/
!*.*
trick to remove binary files with no extension, make sure that ALL other gitignore lines are BELOW. Git will read from .gitignore from the top, so even though I had 'test.go' in my gitignore, it was first in the file, and became 'unignored' after
删除没有扩展名的二进制文件的技巧,确保所有其他 gitignore 行都在下面。Git 将从顶部读取 .gitignore,因此即使我的 gitignore 中有“test.go”,它也是文件中的第一个,并且在之后变为“未忽略”
!*.*