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

tim*_*son 7 git github gitignore

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

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

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

这是我的全部内容.gitignore:

application/config.php
Run Code Online (Sandbox Code Playgroud)

这里是我的一些PHP代码config.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
Run Code Online (Sandbox Code Playgroud)

这是删除我的文件:

  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线长)

tim*_*son 10

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

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

现在http://localhost将保留在我的本地repo副本http://my-site.com的config.php中,并将保存在我的主仓库中的config.php中.

  • 每次我从本地机器提交时,我是否需要这个?或者`update-index`适用于所有未来的提交? (3认同)
  • 这很酷.只是为了让你知道,当你要求A时,人们会回答A; 而你可能想要B. (2认同)