Automatically Creating A Gitlab Repo
I’ve been working on streamlining some of my processes lately. I noticed that one of the one things that takes a little time is creating a new GitLabRepository. It just seemed like it took too much time.
Here are the previous steps
- Goto GitLab Webpage
- Click on new project
- Enter a name
- Select a namespace
- Check the box to initialize a readme
- Click create project
- Click on the clone button
- Click again to copy the address
- Close the browser
- Right Click somewhere to open gitbash
- Enter git clone and right click to paste the address
- HIt enter
At one point I discovered that I could use a push to create a new gitlab repository. That is great, but it still required a few more steps to set the remote and the tracking branch. I decided to wrap it all into a bash script called create. I placed it in C:\Program Files\Git\usr\bin.
Now the command
create namespace reponame will create a local repo and push it to a new repo on GitLab. The script also does some other stuff like change the default branch name and initialize gitflow. All in one command from Git Bash. Also it doesn’t require any API keys or anything like that. All you have to do is have is appropriate permissions in the namespace. If you use ssh it will ask you multiple times for the password for your ssh key, but you can get around that. If you use https your experience may differ, in which case I suggest using ssh.
The only thing left to do is next time you log into GitLab change the default branch to develop and you are all set. One thing to note: the namespace must already exist.
Here is the script:
#!/bin/bash default_branch="main" echo "git init" git init echo "set default branch" git symbolic-ref HEAD refs/heads/$default_branch #create empty initial commit git commit --allow-empty -m"initial empty commit" echo "Push to new repo" git push --set-upstream email@example.com:$1/$2.git $default_branch echo "Add origin" git remote add origin firstname.lastname@example.org:$1/$2.git echo "Fetch" git fetch origin $default_branch echo "Setup tracking branch" git branch -u origin/$default_branch echo "Create develop branch" git switch -C develop echo "Push develop" git push --set-upstream origin develop echo "Setup tracking branch" git branch -u origin/develop # set remote head to develop git remote set-head origin -a # Init gitflow git flow init -d