【问题标题】:How to create a new gitlab repo from my existing local git repo, using CLI?如何使用 CLI 从我现有的本地 git 存储库创建新的 gitlab 存储库?
【发布时间】:2016-01-11 03:53:34
【问题描述】:

我的 OSX 上有许多本地 git 存储库。使用命令行,我想从现有的本地存储库中在 https://gitlab.companyname.com 上创建新的 gitlab 存储库。

有可能吗?

【问题讨论】:

  • 从您的网址我假设您正在运行gitlab server。根据 Gitlab CE 的 documentation,有 third-party appicationsAPI 的包装器/CLI。也许其中之一可能会有所帮助。
  • 我尝试了该页面提供的工具。我不清楚文档
  • 从 GitLab 10.5(2018 年 2 月)开始,可以在 GitLab 上简单地 push to create a new project。 @JitendraVyas 考虑在下面接受 ted 的回答,以提高此解决方案的知名度。
  • 值得注意的是,如果您在 gitlab 上创建一个新项目,那么空白项目会提供详细说明,以各种方式将您的代码放入其中 :-)
  • 注意:在 gitlab 中创建存储库时,它会显示确切的步骤和命令

标签: git gitlab


【解决方案1】:

2018 解决方案:只需使用--set-upstream

假设您将负责编写脚本,为每个本地存储库执行以下操作,似乎从 Gitlab 10.5 开始,您可以简单地使用

# To your own domain
git push --set-upstream address/your-project.git

# To gitlab.com with SSH
git push --set-upstream git@gitlab.example.com:username/new-repo.git master

# To gitlab.com with HTTP
git push --set-upstream https://gitlab.example.com/username/new-repo.git master

这将在 Gitlab 上创建一个新项目您无需在服务器上手动创建它


来自文档

Push to create a new project

当你在本地创建新仓库时,不用在 GitLab 中手动创建新项目然后cloning the repository locally,你可以直接将其推送到 GitLab 以创建新项目,而无需离开终端。如果您有权访问关联的命名空间,GitLab 会自动在该 GitLab 命名空间下创建一个新项目,默认情况下其可见性设置为 Private(您可以稍后在 project's settings 中更改它)。

这可以通过使用 SSH 或 HTTPS 来完成:

## Git push using SSH
git push --set-upstream git@gitlab.example.com:namespace/nonexistent-project.git master

## Git push using HTTPS
git push --set-upstream https://gitlab.example.com/namespace/nonexistent-project.git master

您可以将标志--tags 传递给git push 命令以导出现有的存储库标签。

一旦推送成功完成,一条远程消息会指示设置远程的命令和新项目的 URL:

remote:
remote: The private project namespace/nonexistent-project was created.
remote:
remote: To configure the remote, run:
remote:   git remote add origin https://gitlab.example.com/namespace/nonexistent-project.git
remote:
remote: To view the project, visit:
remote:   https://gitlab.example.com/namespace/nonexistent-project
remote:

【讨论】:

  • 在实践中我最终这样做了:git push --set-upstream https://gitlab.com/myName/myProject.git master。不知道为什么文档使用“gitlab.example.com”。
  • 因为您可以在自己的服务器上托管自己的 gitlab 实例,并在这样的子域上提供服务
  • 我用git push --all --set-upstream https://gitlab.example.com/namespace/nonexistent-project.git推送了所有分支
  • 截至 2021 年 7 月,此解决方案在 git version 2.32.0.windows.2 中完美运行。
【解决方案2】:

如果你使用过nodejs,甚至对它有一个简单的了解,node-gitlab 模块就很棒。在一个自托管的 Gitlab 实例上,我已经能够创建项目并从远程存储库(本地 git 服务器)导入存储库。对于本地存储库,该过程应该类似。您可以在您的机器上设置一个本地 git 服务器,并将其用作每个 Gitlab 项目的 import_url。

或者,您可以编写一个脚本,使用 API 创建项目,然后将每个存储库推送到其各自的项目。

伪代码: 对于目录中的每个 repo:

  • 使用 API 在 gitlab.com 上创建项目
  • git remote add gitlab url-to-gitlab-repo
  • git push gitlab --mirror

【讨论】:

    【解决方案3】:

    我必须同意你的观点,Gitlab 的 API 包装器 third-party applications 的文档并不理想,但我确实设法让其中一个工作。

    为此,我在运行 Ubuntu 14.04 的 vagrant box 中设置了一个沙箱 gitlab 服务器(GitLab Community Edition 8.0.5)。

    现在,我使用的 API 包装器是 this onepython-gitlab by Gauvain Pocentek)。我选择这个是因为它有足够多的人主演(撰写本文时有 118 人),而且它是用 python 编写的,所以可移植性不会成为问题(我的主机是 Windowscygwin em>,但我将使用 unix 语法来回答这个问题)。

    使用pip 安装非常简单:

    $ sudo pip install python-gitlab
    

    一旦安装,您将不得不修改一个配置文件 - 不存在 开箱即用 或者,至少,我找不到它 - (文档不清楚这)。此文件的“官方”名称是 .python-gitlab.cfg,这是 config.py 默认搜索的文件。

    无论如何,我根据项目github 中的示例语法创建了自己的.python-gitlab.cfg 版本,如下所示:

    [global]
    # required setting
    default = local
    # optional settings
    ssl_verify = false
    timeout = 5
    
    [local]
    # url = http://10.0.3.2:8080
    # get the private token from the gitlab web interface
    # private_token = vTbFeqJYCY3sibBP7BZM
    
    [remote]
    url = YOUR SERVER URL GOES HERE
    private_token = YOUR PRIVATE TOKEN GOES HERE
    ssl_verify = false
    
    [remote-ssl]
    url = YOUR HTTPS URL GOES HERE (eg https://gitlab.ccompanyname.com))
    private_token = YOUR PRIVATE TOKEN GOES HERE
    ssl_verify = true (VALID CERTIFICATE) OR false (SELF-SIGNED CERTIFICATE)
    

    您必须从网络界面(在个人资料设置 :: 帐户中找到)为自己获取一个私人令牌,因为正如自述文件指出的那样,

    仅支持私有令牌身份验证(不支持用户/密码)。


    搞定后,创建项目就可以这样实现了,http:

    $ gitlab -c "PATH/TO/YOUR/.python-gitlab.cfg" --gitlab remote project create --name YOUR_PROJECT_NAME
    

    喜欢这个https:

    $ gitlab -c "PATH/TO/YOUR/.python-gitlab.cfg" --gitlab remote-ssl project create --name YOUR_PROJECT_NAME
    

    上面用到的开关,可以通过查看帮助找到:

    $ gitlab --help
    

    现在,假设您已经处理了 SSH 密钥(本地和 Web 界面中的),并且您希望 gitlab 存储库名称与本地 git 中的目录相同,然后,像下面这样一个小bash 脚本,可以自动创建项目和本地 repos 推送:

    #!/usr/bin/bash
    
    cd 'PATH/TO/YOUR/REPOS/DIRECTORY' # enter your local repos dir here
    server="YOUR SERVER" # enter your server URL
    user="YOUR USER" # enter your user name
    gitlab_cfg="PATH/TO/YOUR/.python-gitlab.cfg" # enter the location of config file
    #method="remote" # uncomment for http, comment for https
    method="remote-ssl" # uncomment for https, comment for http
    for i in $( ls -1 ); do 
        echo
        echo
        echo '>> Creating Project'
        gitlab -c $gitlab_cfg --gitlab $method project create --name $i
        echo '>> Project ' $i 'created'
        echo '>> ------'
        cd $i
        li=$( tr '[A-Z]' '[a-z]' <<< $i) # convert dirname to lowercase, safe with older bashes (<4)
        origin="git@$server:$user/$li.git"
        echo ">> Reassigning origin to : $origin"
        git remote rm origin
        git remote add origin $origin
        git remote -v
        echo '>> Pushing local repo to gitlab'
        git push -u origin master
        echo '>> Done'
        echo
        echo
        cd ..
    done
    echo
    echo 'Operation finished'
    

    它的作用是创建以外部本地 git 目录中的目录命名的 gitlab 项目,然后将cd 放入每个项目中,更新源,然后执行推送。

    这里要提一提的是,gitlab 会将repo url 转换为小写,例如sampleRepo001 在repo 的url 中变成samplerepo001;这就是我在脚本中将目录名转换为小写的原因。

    最后,这是一个脚本运行示例:

    提醒一下,如果您想使用此脚本,请在应用到实际生产服务器之前进行彻底测试。


    更新 - 我添加了一些关于如何处理 HTTPS/SSL 的更多信息。

    【讨论】:

      【解决方案4】:

      在 GitLab 中为您要推送到 GitLab 的每个本地存储库创建新的空项目。创建项目后,您将被带到默认项目页面。

      然后 cd 进入您现有的每个 git 存储库。做一个git remote add origin &lt;your new gitlab repo address&gt;

      然后是git push -u origin master

      您需要为要添加的每个存储库执行此操作。

      您的回购地址在项目页面上提供给您。作为 http 和/或 ssh。如果您的本地计算机上已经有一个名为 origin 的远程,您可能需要先重命名它。或者您可以将 gitlab 称为不同的东西。此外,如果您想将所有分支推送到 gitlab,您可以执行 git push --all origin 如果您想要标签,git push --tags origin

      【讨论】:

      • “在 GitLab 中创建新的空项目”这就是问题所在。我想从 CLI 中完成
      • 也许我们应该先git remote remove origin
      猜你喜欢
      • 2019-04-16
      • 1970-01-01
      • 2018-04-30
      • 1970-01-01
      • 2018-06-26
      • 2019-02-14
      • 2013-11-04
      • 2015-09-29
      • 1970-01-01
      相关资源
      最近更新 更多