How to create a new gitlab repo from my existing local git repo, using CLI?
Solution 1:
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 thegit 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:
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