告别 SFTP,用 Git 管理 WordPress 主题、插件和展示页面
| SFTP | Git + Hook | |
|---|---|---|
| 新增文件 | ✅ 手动拖拽 | ✅ git push |
| 修改文件 | ✅ 手动覆盖 | ✅ git push |
| 删除文件 | ❌ 不删,残留 | ✅ 自动同步删除 |
| 回滚 | ❌ 不可能 | ✅ git revert |
| 版本历史 | ❌ 没有 | ✅ 完整 commit 记录 |
| 两端一致性 | ❌ 经常不同步 | ✅ 完全一致 |
HaoYe\haoyeastrachildwp-content/themes/haoyeastrachild/git-repos/haoyeastrachild.gitpublisher/node_modules、publisher/posts
HaoYe\pluginswp-content/plugins/git-repos/plugins.gitakismet、redis-cache、wp-super-cache
HaoYe\showcase/showcase/git-repos/showcase.gitgit push
server master/git-repos/*.gitcheckout -f服务器路径不同。 三个目录分别位于 /showcase、/wp-content/plugins、/wp-content/themes/,单仓库无法映射到三个位置。
更新频率不同。 主题几乎每天改,插件偶尔动,展示页很少变。分开管理,历史清晰。
符合 WordPress 生态惯例。 每个主题、每个插件天然就是独立项目。
checkout -f 会把仓库中没有的文件全部删掉。akismet、redis-cache、wp-super-cache 是 WordPress 后台安装的第三方插件,不在仓库里,每次 push 会被删除。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 中的分支名。