如何忽略与.gitignore位于同一文件夹中的文件

huangapple go评论50阅读模式
英文:

How to ignore a file located in the same folder as .gitignore

问题

My .env file is located in the same folder as my .gitignore file.
Therefore, my .env file is not taken into account and is offered to me to be sent to git. How can I do?

如何忽略与.gitignore位于同一文件夹中的文件

How can i ignore my .env file ?

英文:

My .env file is located in the same folder as my .gitignore file.
Therefore, my .env file is not taken into account and is offered to me to be sent to git. How can I do?

如何忽略与.gitignore位于同一文件夹中的文件

How can i ignore my .env file ?

答案1

得分: 0

你可以将.env行添加到你的.gitignore中,保存更改后它将不会在你的代码库中被跟踪。

如果你已经提交了.env,你可以使用Git的命令行工具移除它

英文:

You can just add .env line to your .gitignore, it will not be tracked in your repository after you save the changes.

If you have already committed .env, you can remove it using Git's CLI.

答案2

得分: 0

如您的截图所示,您的.env文件已被跟踪并且已被修改。
如果文件已被跟踪,那么它就已被跟踪,.gitignore不会产生任何影响。

您可以告诉Git假定文件没有更改,但这是一个非常危险的选项,因为如果您例如切换分支并且文件在分支之间不同等情况下,Git将高兴地覆盖它。

如果您不希望文件被跟踪,您需要取消版本控制它,例如使用git rm --cached .env。并且在.gitignore中,您可以添加/.env以仅匹配在同一级别的文件系统条目。

英文:

As your screenshot shows, your .env file is already tracked and has modifications.
If a file is tracked, it is tracked, and .gitignore does not have any effect whatsoever.

You can tell Git to assume the file being unchanged, but that is a very dangerous option, because then Git will happily overwrite it if you for example switch branches and the file is different between the branches and so on.

If you do not want the file to be tracked, you need to unversion it, e.g. using git rm --cached .env. And to .gitignore you would add /.env to match only the filesystem entry on the same level.

huangapple
  • 本文由 发表于 2023年4月17日 22:56:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76036501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定