🐙 HaoYe Git 部署体系

告别 SFTP,用 Git 管理 WordPress 主题、插件和展示页面

🤔 为什么从 SFTP 换到 Git?

核心痛点:SFTP 只会上传,不会删除。
本地删了文件,服务器依然留着。久而久之,两端文件不一致,到处是僵尸文件。
SFTPGit + Hook
新增文件✅ 手动拖拽git push
修改文件✅ 手动覆盖git push
删除文件❌ 不删,残留✅ 自动同步删除
回滚❌ 不可能git revert
版本历史❌ 没有✅ 完整 commit 记录
两端一致性❌ 经常不同步✅ 完全一致

🏗️ 架构总览

三个独立 Git 仓库

haoyeastrachild 主题
📁 本地 HaoYe\haoyeastrachild
🌐 服务器 wp-content/themes/haoyeastrachild
📦 裸仓库 /git-repos/haoyeastrachild.git
🙈 忽略 publisher/node_modulespublisher/posts
plugins 插件
📁 本地 HaoYe\plugins
🌐 服务器 wp-content/plugins
📦 裸仓库 /git-repos/plugins.git
🙈 忽略 akismetredis-cachewp-super-cache
showcase 展示页
📁 本地 HaoYe\showcase
🌐 服务器 /showcase
📦 裸仓库 /git-repos/showcase.git
🙈 无特殊忽略

数据流

💻 本地
写代码
git push
server master
📦 服务器裸仓库
/git-repos/*.git
🪝 post-receive
hook 触发
checkout -f
检出到网站目录
🌐 haoyelaiga.com
即刻生效

为什么用三个独立仓库而不是一个?

1

服务器路径不同。 三个目录分别位于 /showcase/wp-content/plugins/wp-content/themes/,单仓库无法映射到三个位置。

2

更新频率不同。 主题几乎每天改,插件偶尔动,展示页很少变。分开管理,历史清晰。

3

符合 WordPress 生态惯例。 每个主题、每个插件天然就是独立项目。

🔧 特殊机制

Plugins 仓库:保护第三方插件

问题:checkout -f 会把仓库中没有的文件全部删掉。akismetredis-cachewp-super-cache 是 WordPress 后台安装的第三方插件,不在仓库里,每次 push 会被删除。

解决:post-receive hook 在 checkout -f 前自动备份 .gitignore 中列出的目录,部署后恢复。确保第三方插件既不被追踪,也不会被误删。
# plugins post-receive hook 核心逻辑
# 1. 读取 .gitignore,找出要保留的目录
IGNORE_FILE=$(git show HEAD:.gitignore)
PRESERVE_DIRS=$(echo "$IGNORE_FILE" | grep '/$')

# 2. 备份
cp -r $TARGET/akismet /tmp/plugins-preserve/

# 3. 部署
git checkout -f

# 4. 恢复
cp -r /tmp/plugins-preserve/akismet $TARGET/akismet

📋 日常使用

单个项目部署

在对应目录下:

cd haoyeastrachild    # 或 plugins / showcase
git add -A
git commit -m "描述你的改动"
git push server master

一键全部部署

HaoYe 根目录双击 deploy-all.bat,三个项目依次 push:

[1/3] haoyeastrachild (主题)...  ✅
[2/3] plugins (插件)...        ✅
[3/3] showcase (展示页)...      ✅

更新插件忽略列表

如果要新增忽略的第三方插件(比如装了 contact-form-7):

# 1. 编辑 .gitignore,添加一行
contact-form-7/

# 2. 从 Git 移除追踪(如果之前被追踪了)
git rm --cached -r contact-form-7

# 3. 提交
git add -A && git commit -m "🙈 忽略 contact-form-7"
git push server master

post-receive hook 会自动从 .gitignore 读取新模式并保护该目录。

查看远程地址

git remote -v
# server  ssh://root@111.230.81.144/git-repos/haoyeastrachild.git (push)

🖥️ 服务器文件结构

/git-repos/                    ← 裸仓库集中存放
├── haoyeastrachild.git/
│   └── hooks/post-receive       → 检出到 themes/
├── plugins.git/
│   └── hooks/post-receive       → 检出到 wp-content/plugins/(含备份恢复)
└── showcase.git/
    └── hooks/post-receive       → 检出到 /showcase

/www/wwwroot/haoyelaiga.com/  ← 网站根目录,文件被 hook 自动更新
├── showcase/                    ← git checkout -f 从 showcase.git 部署
└── wp-content/
    ├── plugins/                 ← 从 plugins.git 部署
    │   ├── haoye-announcement-bar/
    │   ├── akismet/             (第三方,被 .gitignore 保护)
    │   └── ...
    └── themes/
        └── haoyeastrachild/     ← 从 haoyeastrachild.git 部署

⚠️ 注意事项

!

不要在服务器上直接改文件。 每次 push 会把服务器文件覆盖为仓库状态。服务器是只读部署目标,所有修改必须在本地完成。

!

WordPress 上传目录不受影响。 wp-content/uploads/ 不在任何仓库中,用户上传的图片等不会丢失。

!

branch 名默认为 master 如果改用 main,需要同步更新 deploy-all.bat 中的分支名。