使用Git在本地保存不同版本的文件,而不是在主存储库中

时间:2012-09-05 02:53:53

标签: git github gitignore

我有一个PHP配置文件,我想在本地操作,但在git commits到我的主存储库期间忽略这些更改。我有一个.gitignore文件,用于忽略这个PHP文件,但是发生了不好的事情,现在config.php文件不再被忽略,我不记得如何重新忽略它。

我知道SO上的人说使用git rm --cached <filename>但是我不能为我的生活弄清楚如何不让git rm...继续删除我的config.php文件。

我想知道是否有人可以列出如何忽略我的config.php,以便我可以继续在本地编辑它,但这些更改不会被添加到回购。

这是我.gitignore的全部内容:

application/config.php

这里是我的config.php中的一些PHP代码我想保留本地,进入我的主仓库:

$config['base_url'] = "http://localhost/";
//$config['base_url'] = "http://mysite.com/"; // this is the code and NOT
// "http://localhost" that i'd like to keep in my repo

这是删除我的文件的内容:

  1. git rm --cached application/config.php
  2. 更改application/config.php文件和其他文件(作为控件)
  3. git commit -m 'config.php shouldn't be changed'

    结果:2 files changed, 1 insertions(+), 370 deletions(-)config.php长370行)

2 个答案:

答案 0 :(得分:9)

我解决了自己的问题。我其实需要这个:

git update-index --assume-unchanged <filename>

假设我的问题中指定了.gitignore和config.php文件, 这是完整的工作流程:

  1. git update-index --assume-unchanged application/config.php
  2. git add -A
  3. git commit -m 'now config.php will be ignored'
  4. git push origin master
  5. 现在http://localhost将保留在我的本地repo副本的config.php中,http://my-site.com将保留在我的主仓库的config.php中。

答案 1 :(得分:0)

您的.gitignore文件中需要config.php

但是,如果您实际上已将config.php添加到您的仓库,则需要git rm然后提交,然后重新创建(或者您可以复制然后复制)

相关问题