在 Linux 下使用 GitLab 同步代码,通常有以下几种常见场景和操作方式。我按最常见的使用方式给你系统讲一下。
sudo apt update
sudo apt install git -y # Ubuntu/Debian
# 或
sudo yum install git -y # CentOS
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
ssh-keygen -t ed25519 -C "你的邮箱"
cat ~/.ssh/id_ed25519.pub
把公钥复制到 GitLab → Settings → SSH Keys
git clone git@gitlab.com:username/project.git
cd project
git pull origin main
(分支名可能是 master)
git add .
git commit -m "更新说明"
git push origin main
git fetch origin
git merge origin/main
git pull --rebase origin main
cd your_project
git init
git remote add origin git@gitlab.com:username/project.git
git add .
git commit -m "init"
git push -u origin main
crontab -e
添加:
*/5 * * * * cd /path/to/project && git pull origin main >> /var/log/gitlab_sync.log 2>&1
git@ 开头git branch -a
git checkout -b main origin/main
git status
# 手动解决冲突后
git add .
git commit
GitLab 自带 Mirror Repository:
如果你能说明:
我可以给你更精确的命令或脚本。