"git push" 删除未跟踪的远程文件

"git push" deletes untracked remote files

我正在 运行 设置一个 Web 服务器,允许用户将图像上传到服务器。但是,我正在使用 git 来管理我的源代码,并且 git push 操作会删除服务器上与我的本地结帐不匹配的任何内容 - 所以我每次我都丢失图像 运行 git push!

起初我认为我可以保护 上传文件夹,所以我按照其他post中的建议尝试了所有这些操作:

None 解决了这个问题 - 当我 git push.

时,远程目录总是消失

接下来,我决定将上传的文件放在git的工作区之外,这样git push就不会删除它。然后我从 public 目录创建了一个 符号 link 到私有目录,这样我就可以看到文件 publicly。到目前为止一切顺利......但是,每当我 运行 git push 它删除符号 link!

最后,我想也许我可以使用 post-receive git 钩子来创建符号 link 每次我推送,但我的网络服务器 (openshift) 已经在使用该挂钩进行其他操作,不允许我对其进行编辑。

肯定有一个简单的方法可以做到这一点?!请帮忙!

是的,您要推送到的 git 存储库不应是具有工作目录的存储库。它应该是一个 bare 存储库,由

启动
git --bare init

如果你还想在目标机器上工作,最好的解决方案是使用两个存储库(一个裸机和一个不裸机),你推到裸机,然后用钩子拉到正常。可以找到此设置的示例 here.

与大多数平台即服务提供商一样,OpenShift 会在您每次 push 时部署应用程序的 全新版本 。默认情况下,任何未被 Git 跟踪的内容都不会包含在新版本中。

您有两个选择:

  1. OpenShift 支持 persistent data storage using a special directory:

    The best practice for storing files that must persist across deployments is to use the 'data' directory that is located one level up from your git repository. You can use the $OPENSHIFT_DATA_DIR env variable to reference this on the host. For example:

    If you create a php app called "bunny". The repo's php/index.php file gets deployed to:

    ~/{cartridge name}/repo/php/index.php

    The data directory, then, gets deployed to

    ~/{cartridge name}/app-root/data/

    Therefore from your php/index.php file, if you save a file to "../../../data/blah.txt" it will get placed outside of your repo directory into the data directory.

  2. 在别处存储生成/上传的资产。

    这是 PaaS 提供商喜欢的方法,例如 Heroku. Instead of storing user uploads and other generated content on the PaaS server, store it on something like Amazon S3

您应该使用部署操作挂钩在 OPENSHIFT_DATA_DIR 中创建符号链接,您可以在此处的 WordPress 快速入门中查看如何执行此操作的示例:https://github.com/openshift/wordpress-example/blob/master/.openshift/action_hooks/deploy

OPENSHIFT_DATA_DIR 在部署之间持续存在,但不会在缩放应用程序中的齿轮之间共享。