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

5 Comments on “Automatically Creating A Gitlab Repo

  1. Nice. I’ll probably incorporate this code on my routine for creating new repos and projects templates. Probably one more post on the blog.
    One question, what are the implications of using HTTPS? The need of inserting password, username? Or this depends on the setup?

  2. I imagine it would just ask you for the login info multiple times. I’m sure there is probably a way to cache it just like you can cache the password for your ssh key.

  3. Sam,

    This is an excellent article suggestion. I wish I had read it before our conversation on Monday. I am definitely going to incorporate it into my processes to save time. One question if you are doing a project that for example is going to use TestStand and LabVIEW together do you put all the code in the same repository or create separate repositories for TEstSTand and LabVIEW?

    • Bill,
      glad you found it useful. I find it a huge timesaver. As far as TestStand and LabVIEW, I have only really done 2 TestStand Projects, so take this with a grain of salt, but in those cases we just used one repository and it worked fine. I don’t think there would necessarily be anything wrong with using seperate repositories, but we just didn’t have a need.

Leave a Reply

Your email address will not be published. Required fields are marked *