简体   繁体   English

如何使用 PhpStorm 和 Git 忽略文件?

[英]How to ignore files with PhpStorm and Git?

I am not able to ignore files which are already under version control.我无法忽略已经在版本控制之下的文件。

The "Ignore..." entry is disabled: “忽略...”条目被禁用:

在此处输入图片说明

1st question: Why is it disabled?第一个问题:为什么它被禁用?

2nd: How to add files afterwards to git ignore?第二:如何在之后添加文件到 git ignore?

Thanks a lot!非常感谢!

Edit编辑

PhpStorm says: "This option is only available if a file under the Unversioned Files node is selected. Use this option to ignore the selected file if you want to leave it unversioned." PhpStorm说:“此选项仅在选择了 Unversioned Files 节点下的文件时才可用。如果您想让它保持未版本化,请使用此选项忽略所选文件。”

(Source: https://www.jetbrains.com/help/phpstorm/2016.3/local-changes-tab.html ) (来源: https : //www.jetbrains.com/help/phpstorm/2016.3/local-changes-tab.html

But sometimes you may have files for an initial commit, then add it to git ignore to allow local changes (for local development) while not committing / pushing this file to any repo anymore.但有时您可能有初始提交的文件,然后将其添加到 git ignore 以允许本地更改(用于本地开发),同时不再将此文件提交/推送到任何存储库。

Edit 2编辑 2

Another way I handle this nowadays is to add the changes with credentials to a Changelist which is not committed:我现在处理这个问题的另一种方法是用凭据未提交变更列表添加的变化:

在此处输入图片说明

It make no sense to ignore files that are already in your version control.忽略版本控制中已经存在的文件是没有意义的。 So you have to remove them first with:所以你必须先删除它们:

git rm --cached filename

then that files exist locally but are removed from your repository.然后该文件存在于本地但从您的存储库中删除。 Then you can ignore that files.然后你可以忽略这些文件。

For the first question, as you found in the link.对于第一个问题,正如您在链接中找到的那样。

For the second question, if you want to ignore a file which has already version controlled by git, you should use git command to do that:对于第二个问题,如果你想忽略一个已经被 git 控制的文件,你应该使用 git 命令来做到这一点:

  1. In the local repo, add the file name in .gitignore file.在本地 repo 中,在.gitignore文件中添加文件名。 If you don't have .gitignore , you can use touch .gitignore .如果你没有.gitignore ,你可以使用touch .gitignore
  2. git rm --cached filename . git rm --cached filename

After reading your comments I think what you really need here is lock file feature, that exists (as far as I know) only on gitlab.阅读您的评论后,我认为您真正需要的是锁定文件功能,该功能(据我所知)仅存在于 gitlab 上。 But... this requires central server so it's actually against git main idea.但是......这需要中央服务器,所以它实际上违背了git的主要思想。

In most cases - as Stony said - you should create template file like config.template.php or config.dev.php , add config.php to .gitignore and make each developer rename/recreate config.php.在大多数情况下 - 正如 Stony 所说 - 你应该创建像config.template.phpconfig.dev.php这样的模板文件,将config.php添加到 .gitignore 并让每个开发人员重命名/重新创建 config.php。

Another way would be to create deploy script with transformation instructions that'll automatically adjust paths inside config file, maybe automatically deploy entire application, and run it at each deploy.另一种方法是使用转换指令创建部署脚本,这些指令将自动调整配置文件中的路径,可能会自动部署整个应用程序,并在每次部署时运行它。

Or simply exclude config file every time you commit changes.或者在每次提交更改时简单地排除配置文件。

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

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