How to create a new gitlab repo from my existing local git repo, using CLI?

GitGitlab

Git Problem Overview


I have many local git repos on my OSX. Using the command line I want to make new gitlab repositories on https://gitlab.companyname.com from existing local repositories.

Is it possible to do that?

Git Solutions


Solution 1 - Git

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

# To your own domain
git push --set-upstream address/your-project.git

# To gitlab.com with SSH
git push --set-upstream [email protected]:username/new-repo.git master

# To gitlab.com with HTTP
git push --set-upstream https://gitlab.example.com/username/new-repo.git master

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 repository locally, instead of manually creating a new project in GitLab and then cloning the repository locally, you can directly push it to GitLab to create the new project, all without leaving your terminal. If you have access rights to the associated namespace, GitLab automatically creates 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 HTTPS:

## Git push using SSH
git push --set-upstream [email protected]:namespace/nonexistent-project.git master

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

> You can pass the flag --tags to the git push command to export existing repository tags. > > Once the push finishes successfully, a remote message indicates 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:

Solution 2 - Git

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

Solution 3 - Git

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:

enter image description here

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.

Solution 4 - Git

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJitendra VyasView Question on Stackoverflow
Solution 1 - GittedView Answer on Stackoverflow
Solution 2 - Gittwk3View Answer on Stackoverflow
Solution 3 - GitsokinView Answer on Stackoverflow
Solution 4 - Gituser3606666View Answer on Stackoverflow