简体   繁体   English

.gitignore没有正确忽略文件

[英].gitignore not ignoring a file properly

I have a project and its structure is following: 我有一个项目,其结构如下:

build/
.git/
.gitignore
LICENSE
pics/
project.c
README.md
shared/
solutions/

My build/ folder is where I use CMake so there are various files like CMakeCache.txt , CMakeLists.txt etc. which I want git to ignore. 我的build/文件夹是我使用CMake的地方,所以有很多文件,例如CMakeCache.txtCMakeLists.txt等,我希望git忽略。

My .gitignore file has: 我的.gitignore文件具有:

.gitignore
*.out
*.swp
/build/*

When I run git status there is modified: CMakeLists.txt which I don't understand. 当我运行git status它被modified: CMakeLists.txt ,我不理解。 Why is CMakeLists.txt not ignored? 为什么不忽略CMakeLists.txt And why is it the only file that's not ignored? 为什么它是唯一不被忽略的文件?

As git ls-files --error-unmatch build/CMakeLists.txt does not show you any error, it means that build/CMakeLists.txt file is already tracked in your repo, so .gitignore file does not affect it anymore. 由于git ls-files --error-unmatch build/CMakeLists.txt不会显示任何错误,这意味着您的存储库中已经跟踪了build/CMakeLists.txt文件,因此.gitignore文件不再影响它。 You need to run git rm --cached build/CMakeLists.txt to delete it from git. 您需要运行git rm --cached build/CMakeLists.txt将其从git中删除。

Ignore rules (like those in .gitignore ) only apply to untracked files. 忽略规则(如.gitignore规则)仅适用于未跟踪的文件。 Changes to tracked files cannot be ignored. 对跟踪文件的更改不能忽略。 There are mechanisms people like to suggest for this use case, but they all cause more trouble than the solve. 人们喜欢针对这种用例提出建议的机制,但是它们都会带来比解决方案更多的麻烦。

(And we know that the file in question is tracked, because git tells you it's modified . That means there's a version of the file already in the index.) (而且我们知道该文件已被跟踪,因为git告诉您它已被修改 。这意味着索引中已经有该文件的版本。)

If you remove the file from the index then ignore rules can apply. 如果从索引中删除文件,则可以应用忽略规则。 That means that from this commit forward, the repo contains no version of the file. 这意味着从此提交开始,存储库不包含文件的任何版本。 If you want ignore to successfully cover the entire build directory, you could say 如果要忽略以成功覆盖整个build目录,可以说

git rm -r --cached build

Of course if the file still exists in other "current" commits (ie on other branches), then it will still be there (and could sneak back into your current branch by way of merges). 当然,如果文件仍然存在于其他“当前”提交中(即,在其他分支上),则它仍将存在(并且可以通过合并偷偷回到您的当前分支中)。

If you never meant the file to be there and can abide a history rewrite, you might consider using BFG Repo Cleaner to fully get rid of it; 如果您从不希望文件在那里并且可以遵守历史记录重写,则可以考虑使用BFG Repo Cleaner完全摆脱它; but it is an extreme solution that will change commit ID's and require any collaborators to replace their repos. 但这是一个极端的解决方案,它将更改提交ID,并要求任何协作者替换其存储库。

As an aside, there is basically no reason to ever put .gitignore itself in the .gitignore file. .gitignore.gitignore ,基本上没有理由将.gitignore本身放入.gitignore文件中。 Normally you want ignore rules shared as part of the repo, but if you don't you can use .git/info/exclude instead of .gitignore . 通常,您希望忽略作为存储库一部分共享的规则,但如果不这样做,则可以使用.git/info/exclude代替.gitignore

You also don't need separate entries for path-based exclusion and extension-based exclusion, unless files with the given extension exist outside the given path. 您也不需要单独的条目来进行基于路径的排除和基于扩展名的排除,除非具有给定扩展名的文件存在于给定路径之外。

Add directory into .gitignore : 将目录添加到.gitignore

echo 'node_modules/' >> .gitignore

Add file into .gitignore : 将文件添加到.gitignore

echo 'config/constants.js' >> .gitignore

then: 然后:

git rm -r --cached node_modules/   

or: 要么:

git rm -r --cached config/constants.js

Add your other files: 添加其他文件:

git add .

or: 要么:

git add --all

Commit it: 提交:

git commit -m ".gitignore is now working"

Finally push your code into the repo. 最后,将您的代码推送到仓库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM