本文记录如何把宝塔面板创建的网站根目录初始化为 Git 仓库,并同步到另一台自建 Git 服务器的指定仓库中。 文中所有域名、用户名、Token、公钥、服务器 IP 均已替换为示例占位,请按自己的真实信息替换。

宝塔网站根目录同步到自建 Git 仓库教程

一、示例信息

请先确认自己的信息。本文使用以下占位符,实际执行时替换成你的真实配置。

项目示例占位说明
网站根目录/www/wwwroot/example.com宝塔创建的网站目录
Git 服务器域名git.example.com自建 Git 平台域名
Git 用户名gituser拥有目标仓库权限的账号
目标仓库gituser/site_repo.git自建 Git 平台里的仓库路径
HTTPS 仓库地址https://git.example.com/gituser/site_repo.git推荐用于宝塔自动任务
Token<YOUR_GIT_ACCESS_TOKEN>仅在输入密码时粘贴,不建议写进仓库地址

安全提醒:不要把 .env、数据库备份、支付密钥、SSH 私钥、Token、缓存备份文件提交到仓库。

二、初始化并推送网站目录

进入宝塔网站根目录,初始化 Git 仓库,并切换到 main 分支。

cd /www/wwwroot/example.com

git init
git checkout -b main

1. 配置忽略文件

先创建 .gitignore,避免把缓存、日志、数据库备份等文件推送到远程仓库。

vi .gitignore

推荐内容:

*.log
*.sql
*.zip
*.tar
*.gz
*.bak

.env
runtime/
cache/
tmp/
temp/
logs/
backup/
*/cache/
*/backup/
super_user/uploadfile/recycle/

2. 配置本仓库提交身份

建议直接写入当前仓库配置,避免宝塔计划任务读取不到全局配置。

git config user.name "gituser"
git config user.email "admin@example.com"
git config push.default simple

3. 提交并推送

git remote add origin https://git.example.com/gituser/site_repo.git

git add .
git commit -m "Initial sync from BT website root"
git push -u origin main

如果之前已经添加过远程仓库,出现 remote origin already exists,使用        git remote set-url origin https://git.example.com/gituser/site_repo.git 修改即可。

三、配置 HTTPS Token

使用 HTTPS 推送时,推荐使用访问令牌 Token 代替登录密码,尤其适合计划任务自动同步。

1. 创建 Token

在 Gitea / Forgejo 页面进入:

右上角头像 -> 设置 -> 应用 -> 访问令牌

创建令牌时推荐配置:

选项推荐值
令牌名称bt-site-sync
仓库和组织访问权限全部,包含公开、私有和受限仓库
repository读写
其他权限无访问权限

2. 在宝塔服务器保存凭据

使用 Git 的凭据保存功能。计划任务用 root 执行时,需要在 root 用户下配置。

git config --global credential.helper store

git ls-remote https://git.example.com/gituser/site_repo.git

命令提示输入账号密码时:

Username: gituser
Password: <YOUR_GIT_ACCESS_TOKEN>

成功后会返回类似:

<commit-hash>    HEAD
<commit-hash>    refs/heads/main

然后保护凭据文件权限:

chmod 600 /root/.git-credentials

不建议把 Token 写进远程地址,例如 https://TOKEN@git.example.com/...。保存在        /root/.git-credentials 并限制权限更稳妥。

四、配置宝塔计划任务

进入宝塔面板:

计划任务 -> 添加任务
配置项建议配置
任务类型Shell 脚本
任务名称网站 Git 自动同步
执行周期按需设置,例如每天 00:30
执行用户root

脚本内容:

export HOME=/root
export GIT_TERMINAL_PROMPT=0

cd /www/wwwroot/example.com || exit 1

git add .

if git diff --cached --quiet; then
  echo "No changes to commit."
  exit 0
fi

git commit -m "Auto sync $(date '+%F %T')" || exit 1
git push || exit 1

如果日志显示 No changes to commit.,说明当前没有变更需要提交,这是正常结果。

五、常见问题处理

1. git add 时出现 CRLF/LF 警告

warning: CRLF will be replaced by LF in xxx

这是换行符提示,不是失败。一般可以忽略。

2. commit 时报没有用户身份

*** Please tell me who you are.
fatal: unable to auto-detect email address

进入仓库目录,设置本仓库专用身份:

cd /www/wwwroot/example.com

git config user.name "gituser"
git config user.email "admin@example.com"
git config push.default simple

3. HTTPS 推送提示无法读取用户名

fatal: could not read Username for 'https://git.example.com': No such device or address

说明计划任务无法交互输入账号密码。需要提前保存 Token 凭据:

git config --global credential.helper store
git ls-remote https://git.example.com/gituser/site_repo.git

4. SSH 推送一直要求输入密码

这通常表示 SSH 公钥没有被 Git 服务器接受。可以用以下命令排查:

ssh -v -i /root/.ssh/id_ed25519 -o IdentitiesOnly=yes -T git@git.example.com

如果日志里出现 Offering public key 但仍然失败,说明服务端没有认可该 key,常见原因包括 key 加错账号、账号没有仓库权限、部署密钥未开启写权限、平台要求验证 SSH key。

5. 老服务器不支持 SSH Key 验证签名

unknown option -- Y

部分旧版 OpenSSH 不支持 ssh-keygen -Y sign。这种情况下可以改用 HTTPS + Token,同样能完成自动推送。

6. 宝塔日志显示 Successful 但 Git 实际失败

宝塔的 Successful 有时只表示脚本进程结束,不代表每条 Git 命令成功。脚本中加入 || exit 1 后,Git 出错时任务会显示失败,方便排查。

六、日常同步命令

手动同步时进入网站目录执行:

cd /www/wwwroot/example.com

git add .
git commit -m "Update website"
git push

查看当前远程仓库地址:

git remote -v

修改远程仓库地址:

git remote set-url origin https://git.example.com/gituser/site_repo.git

查看当前仓库配置的提交身份:

git config --get user.name
git config --get user.email

至此,网站根目录已经可以通过 Git 同步到自建仓库,宝塔计划任务也可以在有变更时自动提交并推送。

文档中所有敏感值均已脱敏,请勿把真实 Token、公钥私钥、服务器 IP、数据库备份或业务密钥写入公开文档。