Automatically Creating A Gitlab Repo

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

  1. Goto GitLab Webpage
  2. Login
  3. Click on new project
  4. Enter a name
  5. Select a namespace
  6. Check the box to initialize a readme
  7. Click create project
  8. Click on the clone button
  9. Click again to copy the address
  10. Close the browser
  11. Right Click somewhere to open gitbash
  12. Enter git clone and right click to paste the address
  13. 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:



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$1/$2.git $default_branch
echo "Add origin"
git remote add origin$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