[英]Is it possible to ignore a file once it is already tracked in Git?
Is it possible to add a dummy configuration file once (so that other developers can see how the configuration file should look) and then ignore it using the .gitignore file. 是否可以一次添加一个虚拟配置文件(以便其他开发人员可以看到该配置文件的外观),然后使用.gitignore文件将其忽略。 Then replace the dummy config details with working ones so I can continue to develop the project and keep committing changes?
然后用可用的配置细节替换虚拟配置细节,这样我就可以继续开发项目并继续提交更改了? But the original dummy config file will remain intact on Github?
但是原始的虚拟配置文件在Github上会保持不变吗?
This is the approach I attempted: 这是我尝试的方法:
But the change to configuration.java is tracked, so this doesn't work. 但是会跟踪对configuration.java的更改,因此这是行不通的。
If you are just worried about ignoring changes in your local dev repo, you can use git update-index --assume-unchanged <file>
to ignore any changes to the file. 如果您只是担心忽略本地开发
git update-index --assume-unchanged <file>
,可以使用git update-index --assume-unchanged <file>
来忽略对该文件的任何更改。 See http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/ for more details. 有关更多详细信息,请参见http://blog.pagebakers.nl/2009/01/29/git-ignoring-changes-in-tracked-files/ 。
You mention : 您提到:
The problem with this is, gitignore only works on files that aren't already being tracked, and since you've committed it it's too late for the gitignore to work. 问题是,gitignore仅适用于尚未被跟踪的文件,并且由于您已提交,因此gitignore的工作为时已晚。 To quote the gitignore docs:
引用gitignore文档:
A gitignore file specifies intentionally untracked files that Git should ignore.
gitignore文件指定Git应该忽略的故意未跟踪的文件。 Files already tracked by Git are not affected;
Git已经跟踪的文件不受影响;
If you want to commit your own work to a publicly visible repo, but want to hide sensitive info such as connection strings / passwords etc, then commit a dummy file first. 如果您想将自己的工作提交到公开可见的仓库中,但又想隐藏诸如连接字符串/密码等敏感信息,请先提交一个虚拟文件。 For example, commit something like this:
例如,提交如下内容:
public class Configuration {
public static final String USERNAME = "someUsername";
public static final String PASSWORD = "passwordHere";
}
Then, tell git to stop tracking changes on that file, so you can amend the values locally but not be prompted to commit those changes. 然后,告诉git停止跟踪该文件上的更改,因此您可以在本地修改值,但不会提示您提交这些更改。
git update-index --assume-unchanged path/to/file.txt
A couple of things to point out: 有两点要指出:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.