Git 推送和部署后,自动生成的文件被删除

After Git push and deployment an auto-generated file gets deleted

我们在 /var/www/ 位置有一个带有 WordPress 项目的 PHP 服务器。整个 /www/ 根目录在 Git 中受源代码控制。因此,当我推送新代码时,它会立即部署,我们会得到新的代码。

在某些时候,我们在服务器上使用图像构建工具,它根据一些数据生成图像,我们将其放在某个包中。

/var/www/images/*.png

所以我在那个位置成功生成了图像; 但是当我添加新的更改并再次部署时 git 从包 .

中删除了所有这些 png 文件

我厌倦了将其添加到 git ignore,但这并没有帮助解决问题。

if I generate image on that location I get the image but when I add new changes and deploy again git remove this all png files from package.

是否取决于部署的完成方式 ()。
,它执行

git --work-tree=/path/to/deploy checkout -f -- .

问题出在 -f/--force option from git checkout:

When switching branches, proceed even if the index or the working tree differs from HEAD. This is used to throw away local changes.

一个解决方案是:

  • 确保在 外部 /path/to/deploy
  • 文件夹中生成 png
  • git checkout -f
  • 之后恢复指向外部文件夹的符号链接

这意味着 post-接收挂钩看起来像:

git --work-tree=/path/to/deploy checkout -f -- .
ln -s /path/to/pngs /path/to/deploy/png