简体   繁体   English

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

[英]“git push” deletes untracked remote files

I am running a web server that allows users to upload images to the server. 我正在运行一个Web服务器,该服务器允许用户将图像上传到服务器。 However, I am using git to manage my source code, and the git push operation deletes anything on the server which doesn't match my local checkout - so I lose the images every time I run git push ! 但是,我使用git来管理源代码,并且git push操作会删除服务器上与本地结帐不匹配的任何内容-因此,每次运行git push时,我都会丢失图像!

At first I thought that I might be able to protect the uploads folder, so I tried all of these things as suggested in other posts: 起初我以为我可以保护上载文件夹,所以我尝试了其他帖子中建议的所有这些操作:

  • adding the directory to .gitignore, 将目录添加到.gitignore,
  • git rm --cached -r uploads
  • git update-index --assume-unchanged uploads

None of these solve the issue - the remote directory always disappears when I do git push . 这些都不能解决问题-当我执行git push时,远程目录总是消失。

Next, I decided to put the uploaded files outside of git's working area, so that git push does not delete it. 接下来,我决定将上传的文件放在git的工作区域之外,以便git push不会删除它。 Then I created a symbolic link from the public directory to the private directory so I can see the files publicly. 然后,我创建了从公共目录到私有目录的符号链接 ,以便可以公开查看文件。 So far so good... However, whenever I run git push it deletes the symbolic link! 到目前为止一切顺利...但是,每当我运行git push它都会删除符号链接!

Finally, I thought that perhaps I could use a post-receive git hook to create the symbolic link every time I push, but my web server (openshift) is already using that hook for something else and won't allow me to edit it. 最后,我认为也许我每次按下时都可以使用接收后 git钩子来创建符号链接,但是我的Web服务器(openshift)已经在将该钩子用于其他操作,并且不允许我对其进行编辑。

There is surely a simple way of doing this?! 肯定有一种简单的方法吗? Please help! 请帮忙!

Like most Platform as a Service providers, OpenShift deploys a whole new version of your application each time you push . 与大多数平台即服务提供商一样,每次push时,OpenShift都会部署应用程序的全新版本 By default, anything not tracked by Git will not be included in the new version. 默认情况下,Git不会跟踪的任何内容都不会包含在新版本中。

You have a couple of options: 您有两种选择:

  1. OpenShift supports persistent data storage using a special directory : OpenShift支持使用特殊目录进行持久数据存储

    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. 存储必须在部署之间持久保存的文件的最佳实践是使用位于git存储库上一层的'data'目录。 You can use the $OPENSHIFT_DATA_DIR env variable to reference this on the host. 您可以使用$ OPENSHIFT_DATA_DIR env变量在主机上引用此变量。 For example: 例如:

    If you create a php app called "bunny". 如果您创建一个名为“ bunny”的php应用程序。 The repo's php/index.php file gets deployed to: 存储库的php / index.php文件被部署到:

    ~/{cartridge name}/repo/php/index.php 〜/ {墨盒名称} /repo/php/index.php

    The data directory, then, gets deployed to 然后,将数据目录部署到

    ~/{cartridge name}/app-root/data/ 〜/ {墨盒名称} / 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. 因此,从php / index.php文件中,如果将文件保存到“ ../../../data/blah.txt”,它将被放置在您的repo目录之外,进入数据目录。

  2. Store generated / uploaded assets elsewhere. 将生成/上传的资产存储在其他位置。

    This is the approach favoured by PaaS providers like Heroku . 这是Heroku等PaaS提供商所青睐的方法。 Instead of storing user uploads and other generated content on the PaaS server, store it on something like Amazon S3 . 与其将用户上传的内容和其他生成的内容存储在PaaS服务器上,不如将其存储在Amazon S3之类的东西上

You should create symlinks into the OPENSHIFT_DATA_DIR using the deploy action hook, you can view a sample of how to do that in the WordPress quickstart here: https://github.com/openshift/wordpress-example/blob/master/.openshift/action_hooks/deploy 您应该使用部署动作挂钩在OPENSHIFT_DATA_DIR中创建符号链接,您可以在以下WordPress快速入门中查看如何执行此操作的示例: https : //github.com/openshift/wordpress-example/blob/master/.openshift/ action_hooks /部署

The OPENSHIFT_DATA_DIR persists between deploys, but is NOT shared between gears in a scaled application. OPENSHIFT_DATA_DIR在部署之间保持不变,但在扩展应用程序中的齿轮之间不共享。

Yes, The git repo you're pushing to should not be a repository with a working directory. 是的,您要推送到的git repo不应是具有工作目录的存储库。 It should be a bare repository which is initiated with 它应该是一个以以下内容启动的bare仓库

git --bare init

If you still want to work on the target machine, the best solution would be to use two repositories (one bare and one not), you push to the bare, and then with a hook, pull to to normal. 如果仍要在目标计算机上工作,最好的解决方案是使用两个存储库(一个为裸仓库,另一个为非仓库),将其推入裸仓库,然后使用钩子将其拉回正常状态。 An example for this setup can be found here . 可以在此处找到此设置的示例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM