简体   繁体   English

一旦文件已经在Git中被跟踪,是否有可能忽略它?

[英]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: 这是我尝试的方法:

  • create repository on Github 在Github上创建存储库
  • intitialise local repo 创新本地回购协议
  • pull remote repo 拉远程回购
  • push local repo to github with Configuration.java (containing dummy details) 使用Configuration.java将本地仓库推送到github(包含虚拟细节)
  • add Configuration.java to the .gitignore file 将Configuration.java添加到.gitignore文件
  • push local repo to github 将本地仓库推送到github
  • add correct details to Configuration.java 向Configuration.java添加正确的详细信息
  • push local repo to github 将本地仓库推送到github

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 : 您提到:

  1. push local repo to github with Configuration.java (containing dummy details) 使用Configuration.java将本地仓库推送到github(包含虚拟细节)
  2. add Configuration.java to the .gitignore file 将Configuration.java添加到.gitignore文件

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: 有两点要指出:

  • Always check in a dummy config file, don't leave it out entirely. 总是检入一个虚拟的配置文件,不要将其完全遗漏。 If you do, when people checkout your project and the file is missing, they won't be able to compile the project! 如果这样做,当人们签出您的项目而文件丢失时,他们将无法编译该项目! Help them by giving some sample data. 通过提供一些示例数据来帮助他们。
  • As git is no longer tracking that file, if you make any modifications that you DO want to commit, you'll have to manually cater to that (either do it via github.com or turn tracking back on, fix it, then disable tracking again). 由于git不再跟踪该文件,因此,如果您要提交要进行的任何修改,则必须手动满足该要求(通过github.com进行,或者重新启用跟踪,修复它,然后禁用跟踪再次)。 For this purpose, keep that file relatively light, just variables, don't put logic in there that you may need to keep updating. 为此,请使该文件保持相对较小的状态,只是变量,不要在其中放置可能需要不断更新的逻辑。

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

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