如何使用CLI从我现有的本地git仓库创建一个新的gitlab仓库?

98

我在我的OSX上有很多本地Git仓库。我想使用命令行从现有的本地仓库在 https://gitlab.companyname.com 上创建新的GitLab仓库。

这是否可行?


从您的URL中我推断您正在运行一个GitLab服务器。根据Gitlab CE的文档,有一些第三方应用程序是API的包装器/CLI。也许其中之一会有所帮助。 - sokin
我尝试了该页面提供的工具。文档对我来说不够清晰。 - Jitendra Vyas
2
自GitLab 10.5版本(2018年2月)起,可以在GitLab上简单地“推送以创建新项目”。@JitendraVyas考虑接受下面ted的答案以提高该解决方案的可见性。 - joelostblom
值得注意的是,如果您在GitLab上创建一个新项目,空白项目将随附有详细说明,介绍各种将代码添加至其中的方法 :-) - Bananaapple
注意:在GitLab中创建存储库时,它会显示确切的步骤和命令。 - Jonathan Pauw
5个回答

104

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上创建一个新项目,无需手动在服务器上创建


来自文档

推送以创建新项目

如果您要在本地创建一个新仓库,而不是手动在GitLab中创建一个新项目,然后克隆该仓库到本地,则可以直接将其推送到GitLab以创建新项目,而无需离开终端。如果您有权限访问相关命名空间,则GitLab会自动在该GitLab命名空间下创建一个默认为私有可见性的新项目(稍后可以在项目设置中更改它)。

可以使用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

您可以在git push命令中使用标志--tags导出现有的存储库标签。

一旦成功完成推送,远程消息将指示设置远程和新项目的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:

4
实际上,我最终执行了这个命令:git push --set-upstream https://gitlab.com/myName/myProject.git master。不确定为什么文档中使用了“gitlab.example.com”。 - mike rodent
2
因为你可以在自己的服务器上托管自己的GitLab实例,并在这样的子域名上提供服务。 - ted
我使用 git push --all --set-upstream https://gitlab.example.com/namespace/nonexistent-project.git 命令将所有分支推送到了远程仓库。 - Rohan 'HEXcube' Villoth
截至2021年7月,这个解决方案在git version 2.32.0.windows.2中完美运行。 - carloswm85

75
为你想要上传到GitLab的每个本地repo创建新的空项目。创建完成后,您将被带到默认的项目页面。
然后,进入您现有的每个git repo. 执行git remote add origin <your new gitlab repo address> 接下来执行 git push -u origin master
您需要为要添加的每个存储库执行此操作。
你的repo地址在项目页面上提供, 包括http和/或ssh协议. 如果您本地机器上已经有一个名为origin的远程,则可能需要先重命名它。或者您可以将gitlab的远程名称设为不同的名称。如果您想将所有分支推送到gitlab,则可以执行git push --all origin 如果您想要标签,请使用git push --tags origin.

16
在GitLab中创建新的空项目。我想要通过CLI完成它。 - Jitendra Vyas
2
也许我们应该先执行 git remote remove origin - MichaelMao

9
我同意您的看法,Gitlab的API包装器第三方应用程序的文档不是很理想,但是我设法使其中一个工作。
为此,我在运行Ubuntu 14.04的vagrant box中设置了一个沙盒gitlab服务器(GitLab Community Edition 8.0.5)。
现在,我使用的API包装器是这个(由Gauvain Pocentek编写的python-gitlab)。我选择它是因为它有足够多的关注者(在撰写本文时为118人),并且它是用Python编写的,因此可移植性不会成问题(我的主机是Windowscygwin,但我将使用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)

你需要从网页界面(在个人资料设置 :: 账户中找到)获取一个私有令牌,因为正如README所指出的那样,只支持私有令牌身份验证(不支持用户名/密码)。

在此处理完之后,可以像这样为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脚本可以自动化项目创建和本地存储库推送:
#!/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目录中找到的dirnames命名的gitlab项目,然后进入每个项目,更新origin并执行push操作。这里要提到的一件事是gitlab将repo url转换为小写,例如sampleRepo001在repo的url中变成samplerepo001,因此我在脚本中将dirnames转换为小写。最后,这是脚本的一个示例运行:

enter image description here

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

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


0
如果您使用nodejs,或者对其有简单的了解,那么node-gitlab模块非常棒。在自托管的Gitlab实例上,我已经能够创建项目并从远程存储库(本地git服务器)导入repos。对于本地存储库,该过程应该类似。您可以在计算机上设置本地git服务器,并将其用作每个Gitlab项目的import_url。
或者,您可以编写一个脚本,使用API创建项目,然后将每个存储库推送到其相应的项目。
伪代码: 对于目录中的每个repo:
  • 使用API在gitlab.com上创建项目
  • git remote add gitlab url-to-gitlab-repo
  • git push gitlab --mirror

0

在 GitLab 中创建一个新的空项目后,它会显示需要执行的命令列表。以下是推送现有本地存储库所需的命令示例。下面显示了 GitLab 提供的各种示例图片。

推送现有 Git 存储库

cd existing_repo
git remote rename origin old-origin
git remote add origin ssh://git@gitlab.companyname.com:1234/existing_repo/project.git
git push -u origin --all
git push -u origin --tags

GitLab 操作示例 GitLab 命令列表


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接