温馨提示×

linux gitlab如何导入项目

小樊
51
2026-10-01 13:14:18
栏目: 智能运维

在 Linux 服务器上的 GitLab 中“导入项目”,通常有以下几种常见场景。我按最常用的情况给你说明。


一、通过 GitLab Web 页面导入(推荐)

1️⃣ 从 GitLab / GitHub / Gitee 等导入

适用于:已有远程仓库(GitHub、GitLab.com、Gitee)

  1. 登录 GitLab
  2. 点击右上角 「New project」
  3. 选择 「Import project」
  4. 选择来源:
    • GitLab.com
    • GitHub
    • Bitbucket
    • 任意 Git 仓库(URL)
  5. 填写原仓库地址和认证信息
  6. 点击 「Import」

✅ 适合不关心历史提交细节、快速迁移


2️⃣ 从本地 Linux 服务器导入(已有代码)

方式 A:先在 GitLab 建空项目,再 push

  1. GitLab 上新建空项目
  2. 在 Linux 本地执行:
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 master

方式 B:导入本地 Git 仓库(含完整历史)

# 确保是 git 仓库
cd your_project
git remote add origin git@gitlab.com:username/project.git
git push --mirror

✅ --mirror 会同步所有分支和 tag


二、通过 Git 命令直接导入(CLI 方式)

从其他 Git 仓库导入

git clone --bare https://github.com/user/repo.git
cd repo.git
git push --mirror git@gitlab.com:username/newrepo.git

适用于:

  • 服务器迁移
  • CI/CD 自动化

三、导入已有代码目录(无 Git)

cd project_dir
git init
git add .
git commit -m "initial commit"
git remote add origin git@gitlab.com:user/project.git
git push -u origin master

四、GitLab 自托管(Linux 服务器)导入说明

如果你是 自己搭建的 GitLab(如 CentOS / Ubuntu):

  • Web 导入路径一致
  • 仓库地址通常是:
http://your-gitlab-ip/username/project.git
或
git@your-gitlab-ip:username/project.git

五、常见问题

❓ SSH 无法 push

# 生成密钥
ssh-keygen -t ed25519
# 把 ~/.ssh/id_ed25519.pub 加到 GitLab

❓ 分支不是 master

git branch -M main
git push -u origin main

如果你能告诉我:

  • ✅ 是 GitLab.com 还是 自建 GitLab
  • ✅ 项目来源(GitHub / 本地 / 其他)
  • ✅ 是否有历史提交

我可以给你精确命令。

0 踩