如何使用 CLI 从我现有的本地 git 存储库创建新的 gitlab 存储库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33101962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create a new gitlab repo from my existing local git repo, using CLI?
提问by Jitendra Vyas
I have many local git repos on my OSX. Using command line I want to make new gitlab repos on https://gitlab.ccompanyname.com, form existing local repos.
我的 OSX 上有许多本地 git 存储库。使用命令行我想在https://gitlab.ccompanyname.com上创建新的 gitlab 存储库,形成现有的本地存储库。
Is it possible to do that?
有可能这样做吗?
回答by twk3
Create new empty projects in GitLab for each of your local repos you want to push to GitLab. After you create the project's, you will be taken to the default project page.
在 GitLab 中为要推送到 GitLab 的每个本地存储库创建新的空项目。创建项目后,您将进入默认项目页面。
Then cd into each of your existing git repos. Do a git remote add origin <your new gitlab repo address>
然后 cd 进入您现有的每个 git 存储库。做一个git remote add origin <your new gitlab repo address>
And then a git push -u origin master
然后一个 git push -u origin master
You will need to do this for each of your repos you want to add.
您需要为要添加的每个存储库执行此操作。
Your repo address is given to you on the project page. As http and/or ssh. If you already have a remote called origin on your local machine, you might want to rename it first. Or you can call the gitlab one something different. Also if you want to push all your branches to gitlab you can do a git push --all origin
If you want your tags, git push --tags origin
您的回购地址在项目页面上提供给您。作为 http 和/或 ssh。如果您的本地机器上已经有一个名为 origin 的远程设备,您可能需要先重命名它。或者您可以将 gitlab 称为不同的东西。此外,如果您想将所有分支推送到 gitlab,您可以执行git push --all origin
如果您想要标签,git push --tags origin
回答by ted
2018 Solution: just use --set-upstream
2018 解决方案:只需使用 --set-upstream
Assuming you'll take care of writing the script that would do the following for each of your local repo, it seems that as of Gitlab 10.5you can simply use
假设您将负责编写脚本来为每个本地存储库执行以下操作,似乎从Gitlab 10.5 开始,您可以简单地使用
git push --set-upstream address/your-project.git
This will create a new project on Gitlab without you creating it manually on the server
这将在 Gitlab 上创建一个新项目,而无需您在服务器上手动创建它
From the Documentation
从文档
When you create a new repo locally, instead of going to GitLab to manually create a new project and then push the repo, you can directly push it to GitLab to create the new project, all without leaving your terminal. If you have access to that namespace, we will automatically create a new project under that GitLab namespace with its visibility set to Private by default (you can later change it in the project's settings).
This can be done by using either SSH or HTTP:
当你在本地新建一个repo时,不用去GitLab手动新建一个项目然后push这个repo,你可以直接push到GitLab来创建新项目,一切都不用离开你的终端。如果您有权访问该命名空间,我们将自动在该 GitLab 命名空间下创建一个新项目,默认情况下其可见性设置为 Private(您稍后可以在项目的设置中更改它)。
这可以通过使用 SSH 或 HTTP 来完成:
## Git push using SSH
git push --set-upstream [email protected]:namespace/nonexistent-project.git master
## Git push using HTTP
git push --set-upstream https://gitlab.example.com/namespace/nonexistent-project.git master
Once the push finishes successfully, a remote message will indicate the command to set the remote and the URL to the new project:
推送成功完成后,远程消息将指示设置远程的命令和新项目的 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:
回答by sokin
I have to agree with you that documentation for Gitlab's API wrapper third-party applicationsis not ideal, however I did manage to make one of them work.
我必须同意您的意见,即 Gitlab 的 API 包装器第三方应用程序的文档并不理想,但是我确实设法使其中一个工作。
For this, I set up a sandbox gitlab server (GitLab Community Edition 8.0.5) in a vagrant box running Ubuntu 14.04.
为此,我在运行Ubuntu 14.04的流浪箱中设置了沙箱 gitlab 服务器(GitLab Community Edition 8.0.5)。
Now, the API wrapper I used is this one(python-gitlabby Gauvain Pocentek). I chose this one because it is starred by enough people (118 at time of writing) and it's written in python so portability will not be an issue (my host machine is Windowswith cygwin, but I will be using unix syntax for this answer).
现在,我的包装使用的API是这一个(上python-gitlab由戈万Pocentek)。我选择这个是因为它被足够多的人加星(在撰写本文时为 118)并且它是用 python 编写的,因此可移植性不会成为问题(我的主机是带有cygwin 的Windows,但我将使用 unix 语法来回答这个问题) .
Installation is quite easy with pip
:
安装非常简单pip
:
$ sudo pip install python-gitlab
Once installed you will have to modify a config file -that does not exist out-of-the-boxor, at least, I could not locate it- (the documentation was not clear about this). This file's "official" name is .python-gitlab.cfg
and this is the one that config.pyis searching by default.
安装后,您将必须修改一个配置文件 -开箱即用,或者,至少,我找不到它 - (文档对此不清楚)。该文件的“官方”的名字.python-gitlab.cfg
,这是一个config.py是默认搜索。
Anyway, I created my own version of .python-gitlab.cfg
based on the sample syntax found at the project's githubwhich goes like this:
无论如何,我.python-gitlab.cfg
根据在项目的github 上找到的示例语法创建了自己的版本,如下所示:
[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)
You will have to get yourself a private token from the web interface (found in Profile Settings:: Account) since, as the README points out,
您必须从 Web 界面(在Profile Settings:: Account 中找到)为自己获取一个私有令牌,因为正如自述文件所指出的那样,
Only private token authentication is supported (not user/password).
仅支持私有令牌身份验证(不支持用户/密码)。
After this is taken care of, creating a projectcan be achieved like this, for http
:
处理完这些之后,就可以像这样创建一个项目,对于http
:
$ gitlab -c "PATH/TO/YOUR/.python-gitlab.cfg" --gitlab remote project create --name YOUR_PROJECT_NAME
and like this for https
:
像这样https
:
$ gitlab -c "PATH/TO/YOUR/.python-gitlab.cfg" --gitlab remote-ssl project create --name YOUR_PROJECT_NAME
The switches used above, can be found by looking at the help:
上面使用的开关,可以通过查看帮助找到:
$ gitlab --help
Now, assumingthat you have taken care of SSH keys (both locally and in the web interface), and that you want the gitlab repo names to be the same as the directories in your local git, then, a little bash
script like the following, can automate the project creation and the local repos push:
现在,假设您已经处理了 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'
What it does is create gitlab projects named after the dirnames found at the outer local git directory, then cd
's into each one of them, renews the origin and then performs the push.
它所做的是创建以在外部本地 git 目录中找到的 dirnames 命名的 gitlab 项目,然后cd
's 进入每个项目,更新原点,然后执行推送。
One thing to mention here is that gitlab converts repo urls to lowercase, for example sampleRepo001
becomes samplerepo001
in the repo's url; that's why I convert dirnames to lowercase in the script.
这里要提到的一件事是 gitlab 将 repo url 转换为小写,例如sampleRepo001
变成samplerepo001
repo 的 url;这就是我在脚本中将目录名转换为小写的原因。
And, finally, here is an example run of the script:
最后,这是脚本的示例运行:
As a reminder, if you want to use this script, test thoroughly before applying to the actual production server.
提醒一下,如果要使用此脚本,请在应用于实际生产服务器之前进行彻底测试。
Update- I added some more info on how to handle HTTPS/SSL.
更新- 我添加了一些关于如何处理 HTTPS/SSL 的更多信息。
回答by user3606666
If you use nodejs, or even have a simple understanding of it, the node-gitlabmodule is great. On a self hosted Gitlab instance I've been able to create projects and import the repos from a remote repository (a local git server). The process should be similar for a local repository. You could setup a local git server on your machine and use that as the import_url for each Gitlab project.
如果你使用 nodejs,甚至对它有一个简单的了解,node-gitlab模块是很棒的。在自托管的 Gitlab 实例上,我已经能够创建项目并从远程存储库(本地 git 服务器)导入存储库。对于本地存储库,该过程应该类似。您可以在您的机器上设置一个本地 git 服务器,并将其用作每个 Gitlab 项目的 import_url。
Or, you can write a script that will use the API to create the project then push each repository to its respective project.
或者,您可以编写一个脚本,使用 API 创建项目,然后将每个存储库推送到其各自的项目。
Pseudocode: for each repo in directory:
伪代码:对于目录中的每个 repo:
- Use API to create project on gitlab.com
- git remote add gitlab url-to-gitlab-repo
- git push gitlab --mirror
- 使用 API 在 gitlab.com 上创建项目
- git remote 添加 gitlab url-to-gitlab-repo
- git push gitlab --mirror