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

时间:2015-10-13 11:50:22

标签: git gitlab

我的OSX上有很多本地git回购。使用命令行我想在https://gitlab.ccompanyname.com上创建新的gitlab repos,形成现有的本地存储库。

有可能吗?

4 个答案:

答案 0 :(得分:46)

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

然后cd进入每个现有的git repos。做一个git remote add origin <your new gitlab repo address>

然后是git push -u origin master

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

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

答案 1 :(得分:22)

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

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

git push --set-upstream address/your-project.git

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

来自文档

Push to create a new project

  

当你在本地创建一个新的repo时,你可以直接将它推送到GitLab来创建新项目,而无需离开你的终端,而不是去GitLab手动创建一个新项目然后推送回购。如果您有权访问该命名空间,我们将自动在该GitLab命名空间下创建一个新项目,默认情况下其可见性设置为Private(您可以稍后在项目的设置中更改它)。

     

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

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

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

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

答案 2 :(得分:9)

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

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

现在,我使用的API包装器是this one python-gitlab Gauvain Pocentek )。我之所以选择这个是因为它有足够多的人(在撰写本文时为118)并且用python编写,所以可移植性不会成为问题(我的主机是 Windows with cygwin ,但我会在这个答案中使用unix语法。)

使用pip安装非常简单:

$ sudo pip install python-gitlab

安装完成后,您将不得不修改配置文件 - 不存在开箱即用,或者至少我无法找到它 - (文档不清楚)这个)。这个文件&#34;官方&#34;名称为.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 repo名称与本地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目录中找到的dirnname命名的gitlab项目,然后cd进入每个目录,更新原点然后执行推送。

这里要提到的一件事是gitlab将repo urls转换为小写,例如sampleRepo001在repo的url中变为samplerepo001;这就是我在剧本中将dirnames转换为小写的原因。

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

enter image description here

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

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

答案 3 :(得分:1)

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

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

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

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