简体   繁体   English

如何在本地工作目录,远程工作目录和git ftp目标中拥有不同版本的文件?

[英]How can I have different versions of a file in the local working directory, remote working directory and git ftp target?

I'm using Git to manage a CodeIgniter project. 我正在使用Git管理CodeIgniter项目。 I work on my local machine and push to a remote server, where I test everything. 我在本地计算机上工作,然后推送到远程服务器,在此测试所有内容。 Then I use git ftp push to update the production site on another host. 然后,我使用git ftp push来更新另一台主机上的生产站点。

Now, to make this work I need three different index.php files, one for every environment, where I just set a different environment variable. 现在,要进行此工作,我需要三个不同的index.php文件,每个文件用于一个环境,在这里我只是设置了一个不同的环境变量。

So I need to gitignore the index.php file, and even git-ftp-ignore it. 所以我需要gitignore index.php文件,甚至git-ftp-ignore。 For the ftp it is working. 对于ftp,它正在工作。 For the normal gitignore it isn't: It keeps updating the index.php file on the remote server. 对于普通的gitignore不是:它会不断更新远程服务器上的index.php文件。

I tried doing git rm --cached index.php , but what I get is that it completely deletes the index.php file on the remote server. 我尝试做git rm --cached index.php ,但是得到的是它完全删除了远程服务器上的index.php文件。

What am I doing wrong? 我究竟做错了什么?

Edit: It seems it's not something I can do with git, but I've found a better way to set the CodeIgniter environment variable . 编辑:看来这不是我可以用git做的,但是我找到了一种更好的方法来设置CodeIgniter环境变量

Firstly, the ignore mechanism doesn't have any effect on files that are already being tracked in the repository. 首先,忽略机制对存储库中已被跟踪的文件没有任何影响。 So, if you want the .gitignore to work, you have to remove the file from the repository (as you did, with git rm --cached ) but then, as you observed, it'll be deleted when you update the remote repository. 因此,如果您希望.gitignore正常工作,则必须从存储库中删除文件(就像使用git rm --cached ),但是正如您观察到的那样,当更新远程存储库时,它将被删除。 。

In summary, I don't think you should .gitignore the file in this situation. 总而言之,在这种情况下,我认为您不应该.gitignore文件。 There's a subtlety about the semantics of ignored files in git which has caught me out number of times - ignored files are regarded as disposable , since the mechanism is designed for build products. 关于git中被忽略文件的语义的微妙之处使我无数次-被忽略的文件被认为是一次性的 ,因为该机制是为构建产品而设计的。 What you have here is a file that is you want to be ignored, but is precious, and git currently doesn't have a way of specifying that. 您所拥有的是一个您要忽略的文件,但是它很宝贵,而git当前没有指定该文件的方法。

For example, in your situation, if you recreate index.php manually, that will appear to all work fine, but then if you use git checkout old-commit to move back to an old version where index.php was tracked, and then go back to where you were before, with git checkout - , your index.php will be deleted! 例如,在您的情况下,如果您手动重新创建index.php ,则看起来一切正常,但是如果您使用git checkout old-commit移回到跟踪index.php的旧版本,然后转到回到以前的位置,通过git checkout - ,您的index.php将被删除!

I would suggest that you change your index.php to source the right setting for the environment variable from some other file which is either: 我建议您更改index.php以从其他一些文件中为环境变量获取正确的设置:

  1. outside the repository 仓库外
  2. or is in the repository, ignored, and at a path that has never been tracked in the repository 或位于存储库中,被忽略,并且位于存储库中从未跟踪过的路径

Ideally, then also add a default value for when that other file doesn't exist. 理想情况下,然后为该其他文件不存在时添加一个默认值。

This is an old question, but I do this as well and have struggled with it in the past. 这是一个古老的问题,但我也这样做,并且过去一直在努力。

What is happening is that since index.php wasn't originally in your .gitignore, index.php is being tracked by all of your repositories. 发生的事情是,由于index.php最初不在您的.gitignore中,因此所有存储库都将跟踪index.php。

When you updated the .gitignore on your development site and removed it from your repository, what it does is passed the "deleted index.php" to your production site. 当您在开发站点上更新.gitignore并将其从存储库中删除时,其作用是将“已删除的index.php”传递给生产站点。 If your production site is still missing index.php on ITS .gitignore, it will "delete" index.php as it should. 如果您的生产站点仍在ITS .gitignore上缺少index.php,它将按需“删除” index.php。

What you then have to do is ensure you add index.php to every .gitignore on every branch of every repository. 然后,您要做的是确保将index.php添加到每个存储库每个分支上的每个.gitignore中。 THEN you can use rm --cached and everything will work as expected. 然后,您可以使用rm --cached,一切都会按预期进行。

暂无
暂无

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

相关问题 如何查找本地Git工作目录中的文件删除时间? - How can I find when a file was deleted in a local Git working directory? git branch -r在不同的本地工作目录中显示不同的远程共享仓库 - git branch -r shows different remote shared repo in different local working directory .git 本地存储库是否必须包含在本地工作目录中? - Does the .git local repository have to be contained in the local working directory? 在git中打开HEAD和二进制文件的工作目录版本 - Open HEAD and working directory versions of a binary file in git 在Bootcamp下共享驱动器上使用Git工作目录时,如何防止文件模式更改? - How can I prevent file mode changes when working with a Git working directory on a shared drive under Bootcamp? Git:在本地和远程存储库中保留一个不同的目录吗? - Git: keep a directory different in local and remote repository? 远程更改后,如何将本地工作目录带到任何git修订版,无论其分支如何? - How to take local working directory to any git revision irrespective of its branch after remote changes? 如何将特定目录提交到与git上工作分支不同的目录? - How can I commit specific directory to a different than a working branch on git? 如何在不同的驱动器上设置工作目录和本地存储库? - How can I set up the working directory and the local repository on different drives? Git -- 将先前的提交克隆到与本地工作目录不同的文件夹中 - Git -- cloning a previous commit in a different folder than local working directory
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM