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

发布于 2024-11-30 22:17:36 字数 581 浏览 0 评论 0原文

我正在使用 Git 来管理 CodeIgniter 项目。我在本地计算机上工作并推送到远程服务器,在那里我测试所有内容。然后我使用 git ftp push 更新另一台主机上的生产站点。

现在,为了完成这项工作,我需要三个不同的 index.php 文件,每个环境一个,我只需设置一个不同的环境变量。

所以我需要 gitignore index.php 文件,甚至 git-ftp-ignore 它。对于 ftp 来说它正在工作。对于普通的 gitignore 来说不是:它不断更新远程服务器上的 index.php 文件。

我尝试执行 git rm --cached index.php ,但我得到的是它完全删除了远程服务器上的 index.php 文件。

我做错了什么?

编辑:这似乎不是我可以用 git 做的事情,但是 我找到了一种更好的方法来设置 CodeIgniter 环境变量

I'm using Git to manage a CodeIgniter project. 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.

Now, to make this work I need three different index.php files, one for every environment, where I just set a different environment variable.

So I need to gitignore the index.php file, and even git-ftp-ignore it. For the ftp it is working. For the normal gitignore it isn't: It keeps updating the index.php file on the remote server.

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放低过去 2024-12-07 22:17:37

首先,忽略机制对存储库中已跟踪的文件没有任何影响。因此,如果您希望 .gitignore 工作,则必须从存储库中删除该文件(就像您使用 git rm --cached 所做的那样),但是,您观察到,当您更新远程存储库时,它将被删除。

总之,我认为在这种情况下您不应该 .gitignore 该文件。 git 中忽略文件的语义有一个微妙之处,这让我多次感到困惑 - 忽略文件被视为一次性,因为该机制是为构建产品而设计的。这里有一个你想被忽略的文件,但它很珍贵,而 git 目前没有办法指定它。

例如,在您的情况下,如果您手动重新创建 index.php ,那么看起来一切正常,但是如果您使用 git checkout old-commit 移回到跟踪 index.php 的旧版本,然后使用 git checkout - 返回到之前的位置,您的 index.php 将被删除!

我建议您更改 index.php 以从其他文件中获取环境变量的正确设置,该文件是:

  1. 在存储库外部
  2. 或在存储库中,被忽略,并且位于以下路径: 理想情况下,从未在存储库中跟踪过

,然后还为其他文件不存在时添加默认值。

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.

In summary, I don't think you should .gitignore the file in this situation. 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. 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.

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!

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:

  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.

小…楫夜泊 2024-12-07 22:17:37

这是一个老问题,但我也这样做,并且过去一直在努力解决这个问题。

发生的情况是,由于 index.php 最初并不在您的 .gitignore 中,因此您的所有存储库都在跟踪 index.php。

当您更新开发站点上的 .gitignore 并将其从存储库中删除时,它所做的是将“已删除的 index.php”传递到您的生产站点。如果您的生产站点在 ITS .gitignore 上仍然缺少 index.php,它将按其应有的方式“删除”index.php。

然后您要做的就是确保将 index.php 添加到每个存储库的每个分支上的每个 .gitignore 中。然后你可以使用 rm --cached ,一切都会按预期工作。

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.

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. If your production site is still missing index.php on ITS .gitignore, it will "delete" index.php as it should.

What you then have to do is ensure you add index.php to every .gitignore on every branch of every repository. THEN you can use rm --cached and everything will work as expected.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文