如何克隆私有 GitLab 存储库?

How can I clone a private GitLab repository?

当我这样做时:

git clone https://example.com/root/test.git

我收到这个错误:

fatal: HTTP request failed

当我使用 SSH 时:

git clone username git@example.com:root/test.git

我收到这个错误:

Initialized empty Git repository in /server/user/git@example.com:root/test.git/.git/
fatal: 'user' does not appear to be a git repository
fatal: The remote end hung up unexpectedly

这是一个私有存储库,我已经添加了我的 SSH 密钥。

你的 ssh clone 语句有误:git clone username git@example.com:root/test.git

该语句将尝试将名为 username 的存储库克隆到相对于您当前路径 git@example.com:root/test.git 的位置。

您想省略 username:

git clone git@example.com:root/test.git

如果您尝试使用 GitHub,您可以输入 SSH 来执行此操作:

git clone https://username@github.com/username/repository

对于 GitLab 的基于 HTTPS 的克隆,似乎没有直接的解决方案。因此,如果您想要基于 SSH 的克隆,您应该考虑接下来的三个步骤:

  • 使用您用于注册的电子邮件正确创建 SSH 密钥。我会使用默认文件名来键入 Windows。别忘了输入密码! (提示:如果您已经有一个 ssh 密钥,可以跳过此步骤

     $ ssh-keygen -t rsa -C "your.email@example.com" -b 4096
    
     Generating public/private rsa key pair.
     Enter file in which to save the key ($PWD/.ssh/id_rsa): [\n]
     Enter passphrase (empty for no passphrase):[your password]
     Enter same passphrase again: [your password]
     Your identification has been saved in $PWD/.ssh/id_rsa.
     Your public key has been saved in $PWD/.ssh/id_rsa.pub.
    
  • 从您的 GitLab 配置文件中复制并粘贴最近 id_rsa.pub 生成的所有内容到 Setting>SSH keys>Key

     # Copy to clipboard
     pbcopy < ~/.ssh/id_rsa.pub
    
  • 获取本地连接:

     $ ssh -i $PWD/.ssh/id_rsa git@gitlab.com
    
     Enter passphrase for key "$PWD/.ssh/id_rsa": [your password]
     PTY allocation request failed on channel 0
     Welcome to GitLab, you!
     Connection to gitlab.com closed.
    

最后,克隆任何私有或内部 GitLab 存储库!

$ git clone https://git.metabarcoding.org/obitools/ROBIBarcodes.git

Cloning into 'ROBIBarcodes'...
remote: Counting objects: 69, done.
remote: Compressing objects: 100% (65/65), done.
remote: Total 69 (delta 14), reused 0 (delta 0)
Unpacking objects: 100% (69/69), done.

做之前

git clone https://example.com/root/test.git

确保您已在系统中添加 ssh 密钥。按照这个:https://gitlab.com/profile/keys .

一旦添加运行以上命令。它将提示您输入 gitlab 用户名和密码,并在身份验证时将其克隆。

您可能需要 ~/.ssh/config:

Host gitlab.YOURDOMAIN.DOMAIN
    Port 1111
    IdentityFile ~/.ssh/id_rsa

然后就可以使用git clone git@DOMAINandREPOSITORY了。这意味着您始终使用用户 git.

如果您正在使用 Windows,

  1. 创建一个文件夹并从那里打开 git bash

  2. 在gitbash,

    git 克隆 git@gitlab.com:Example/projectName.git

我尝试了所有这些建议。这是最终对我有用的东西:

  1. https://gitlab.com/-/profile/personal_access_tokens 创建一个访问令牌。 注意:一定要复制令牌并保存。你会需要它的!
  2. git clone https://gitlab.com/USERNAME/REPO.git(用您的唯一信息替换 USERNAME 和 REPO)。
  3. 在请求时输入您的 GitLab 用户名。
  4. 当它要求您输入密码时,请输入您在第 1 步中创建的访问令牌。您的 GitLab 帐户密码对此不起作用。访问令牌就是你想要的。

我创建这个 tool 使用 python 和 docker 一次克隆 GitLab 项目,它将保持 group/subgroup 树结构并将 clone/pull 所有未镜像的 GitLab 存储库。它需要 docker 和 docker 以及来自您的 gitlab 用户的个人访问令牌以及将被视为您希望从中克隆的顶级组的组 ID。