How to create a new gitlab repo from my existing l

2020-02-02 04:45发布

问题:

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.

Is it possible to do that?

回答1:

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.

Then cd into each of your existing git repos. Do a git remote add origin <your new gitlab repo address>

And then a 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



回答2:

2018 Solution: just use --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.5 you can simply use

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

This will create a new project on Gitlab without you creating it manually on the server


From the Documentation

Push to create a new project

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:

## 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

Once the push finishes successfully, a remote message will indicate the command to set the remote and the URL to the new project:

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:


回答3:

I have to agree with you that documentation for Gitlab's API wrapper third-party applications is not ideal, however I did manage to make one of them work.

For this, I set up a sandbox gitlab server (GitLab Community Edition 8.0.5) in a vagrant box running Ubuntu 14.04.

Now, the API wrapper I used is this one (python-gitlab by 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 Windows with cygwin, but I will be using unix syntax for this answer).

Installation is quite easy with pip:

$ sudo pip install python-gitlab

Once installed you will have to modify a config file -that does not exist out-of-the-box or, 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.py is searching by default.

Anyway, I created my own version of .python-gitlab.cfg based on the sample syntax found at the project's github which goes like this:

[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,

Only private token authentication is supported (not user/password).


After this is taken care of, creating a project can be achieved like this, for http:

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

and like this for 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, assuming that 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:

#!/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.

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.

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.



回答4:

If you use nodejs, or even have a simple understanding of it, the node-gitlab module 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.

Or, you can write a script that will use the API to create the project then push each repository to its respective project.

Pseudocode: for each repo in directory:

  • Use API to create project on gitlab.com
  • git remote add gitlab url-to-gitlab-repo
  • git push gitlab --mirror


标签: git gitlab