我在我的OSX上有很多本地Git仓库。我想使用命令行从现有的本地仓库在 https://gitlab.companyname.com
上创建新的GitLab仓库。
这是否可行?
我在我的OSX上有很多本地Git仓库。我想使用命令行从现有的本地仓库在 https://gitlab.companyname.com
上创建新的GitLab仓库。
这是否可行?
--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:
git push --set-upstream https://gitlab.com/myName/myProject.git master
。不确定为什么文档中使用了“gitlab.example.com”。 - mike rodentgit push --all --set-upstream https://gitlab.example.com/namespace/nonexistent-project.git
命令将所有分支推送到了远程仓库。 - Rohan 'HEXcube' Villothgit version 2.32.0.windows.2
中完美运行。 - carloswm85git remote add origin <your new gitlab repo address>
接下来执行 git push -u origin master
。git push --all origin
如果您想要标签,请使用git push --tags origin
.git remote remove origin
。 - MichaelMaopip
安装非常容易:$ 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
#!/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'
更新 - 我添加了一些关于如何处理HTTPS/SSL的更多信息。
在 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 操作示例