This shell script, with only a few configurations, has the goal of initializing a project on Gitlab.com without having to go through the go through the website's ui.

This means: Let's say you just a project with a few source files, but you haven't done anything with git yet. The goal of this script is to allow you to write something like git-init-remote <my_repository_name> on the command line and have both the project on your personal gitlab.com and the local git repository.


token=<YOUR-PRIVATE-TOKEN> # You get this from https://gitlab.com/-/profile/personal_access_tokens

test -z $repo && echo "Repo name required." 1>&2 && exit 1

curl --request POST --header "Private-Token: $token" --header "Content-Type: application/json" --data "{\"name\":\"$repo\"}" "https://gitlab.com/api/v4/projects"

echo "\nDone initializing on remote."
echo "\nCreating local git repository..."

git init
git remote add origin https://gitlab.com/$username/$repo.git
git add .
git commit -m "Initial commit"
git push -u origin master

echo "\nDone."

I would like to have any of your thoughts on how this can break and how scalable this can go. I had trouble finding similar scripts on the web so I felt like a had to write here.


1 Answer 1


Welcome to CodeReview.SE and thanks for submitting your script for a code review!

Good points

  • Good output conveying how progress is being made.
  • Clear variable names


  • Are there limitations for what characters can be in a repo name? If so, you might want to check that before sending it to the API. It is good that you're checking to make sure they passed something in.
  • Aside from making sure $1 is set, there is no error checking. If the curl fails do you want to continue with the git commands?
  • It isn't clear if you want this to run in bash or POSIX sh. The question is tagged for bash, but your sh-bang line points at POSIX shell. But yay for including the sh-bang line.
  • One shell best practice is putting your variable substitutions in double quotes. git remote add origin "https://gitlab.com/$username/$repo.git" shows how to change one of your lines of code. You want the variables inside of double quotes so that spaces in a variable name don't cause the shell to split what you think is one argument into multiple arguments to the command.


  • Do you really want the token stored in the source code? Can you pull this from a secrets vault or store it in a separate file? If you want to commit your source code to a code repository (like git) it would be much better if it didn't include secret tokens.
  • I'd try seeing if I could get this to work as a git subcommand. Here is a medium article on writing your own git subcommands.
  • Use shellcheck to get automated best practice suggestions for your shell code. It can also be run locally on the command line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.