在 Linux 环境下使用 GitLab 同步仓库,通常有以下几种常见场景。你可以根据具体需求选择对应方案。
git clone https://gitlab.com/username/repo.git
# 或 SSH
git clone git@gitlab.com:username/repo.git
进入目录:
cd repo
git pull origin main
(main 可替换为你的分支名,如 master)
git add .
git commit -m "update"
git push origin main
git remote -v
git remote add origin https://gitlab.com/username/repo.git
git push -u origin main
适用于:
步骤:
✅ 优点:无需人工操作
❌ 缺点:需要 GitLab 管理员权限(部分功能仅限付费版)
例如:GitLab A ↔ GitLab B
git clone https://gitlab.com/A/repo.git
cd repo
git remote add gitlabB https://gitlab.com/B/repo.git
# 从 A 推到 B
git push gitlabB main
# 从 B 拉到 A
git pull gitlabB main
git push origin main
例如每天凌晨同步一次:
crontab -e
添加:
0 0 * * * cd /path/to/repo && git pull origin main && git push gitlabB main
.gitlab-ci.yml 示例:
sync:
script:
- git remote add mirror https://github.com/user/repo.git
- git push mirror main
git remote set-url origin https://<token>@gitlab.com/user/repo.git
git branch -a
git fetch --all
如果你能说明:
我可以给你更精确的方案。