Automate Creating a New GitHub Repository with “Gitstart” | by Shinichi Okada | Towards Data Science

[Update: 2021–06–09][Update: 2021–05–23][Updated: 2021–4–17]
(Homebrew version and the newer version are available.)
The Gitstart will remove all the hassle when creating a new GitHub repository. After creating a repository at GitHub, you have to type the following as a standard procedure:
Line 1: Adding “My Repo” to the README markdown file.Line 2: Creating a new Git repository.Line 3: Adding the README.md in the working directory to the staging area.Line 4: Saving your changes to the local repository.Line 5: Creating a branch “main”.Line 6: Adding the remote where your repository is stored at.Line 7: Uploading the local repository content to a remote repository.
I created a bash script called Gitstart which automates the above workflow and adds .gitignore, README, and license.txt to your repo.
Gitstart will create .gitignore and the template README, gitignore, and license file. Then it will add, commit, and push them to your Github account.
GitHub CLI
You need GitHub CLI. It is available for macOS, Linux, and Windows.
gh is GitHub on the command line. It brings pull requests, issues, and other GitHub concepts to the terminal ... — GitHub CLI
GitHub CLI has auth, repo, and other methods. It allows us to log in and create a GitHub repo on the command line.
Once you installed GitHub CLI, you can log in to your GitHub account.
$ gh auth login? What account do you want to log into? GitHub.com- Logging into github.com? How would you like to authenticate? Login with a web browser! First copy your one-time code: 1111-2222- Press Enter to open github.com in your browser...✓ Authentication complete. Press Enter to continue...? Choose default git protocol SSH- gh config set -h github.com git_protocol ssh✓ Configured git protocol✓ Logged in as shinokada
Please select SSH as the default git protocol.
yq
GitHub CLI creates a YAML file at ~/.connfig/gh/hosts.yml. We use yq to read the username from hosts.yml. The yq is a lightweight and portable command-line YAML processor.
$ brew install yq
If you use Homebrew:
brew tap shinokada/gitstart && brew install gitstart
After installing Awesome package manager:
awesome install shinokada/gitstart
Download the Gitstart file or cron the repo.
I recommend creating the ~/bin directory and moving the script to this directory.
$ mkdir ~/bin$ mv ./gitstart ~/bin
Add ~/bin to your PATH in ~/.bashrc or ~/.zshrc, so that you can run the script from anywhere.
export PATH="~/bin:$PATH"
Make the file executable.
$ cd ~/bin$ chmod 755 gitstart
Create a new directory for your new project and then run gitstart. It will ask "Visibility" and "This will create 'your_repo' in your current directory. Continue?".
When you add a language like gitstart -l python, it will search for a gitignore file on the Github official site.
On your terminal:
$ gitstart -d ./my-repo
This will create a directory and a GitHub repo called my-repo.
You can cd to a directory and run gitstart -d . -l python.
$ cd my_new_repo$ gitstart -d . -l python>>> Your github username is shinokada.Select a license:1) MIT: I want it simple and permissive.2) Apache License 2.0: I need to work in a community.3) GNU GPLv3: I care about sharing improvements.4) QuitYour lisence: 2Apache>>> Creating .gitignore for Python...
You need to select one of the licenses.
Outputs:
The script will:
Read your license number and create a license.txt.
Read your GitHub user from ~/.config/gh/hosts.yml.
If a language is provided, it will check at the Github gitignore site if the language exists or not. If the status is 200 then it will create a .gitignore file.
Uses the directory name as a GitHub repo name.
Run git init and create a README.md file with the repo name.
Add README.md and commit with a message.
If you are logged in, create a new repo at GitHub.com.
Add the remote and push all the files in the directory.
The following line store your Github username to user.
user=$(cat <"$HOME"/.config/gh/hosts.yml | yq r - '"github.com".user')
The following will store the HTTP status code to http_status.
url=https://www.google.com/http_status=$(curl --write-out '%{http_code}' --silent --output /dev/null "$url")
ShellCheck is a shell script static analysis tool. It points out and clarifies typical syntax issues that cause a shell to give cryptic error messages. You can find your installation here. I used it to improve my bash syntax.
$ cd ~/bin$ shellcheck gitstartIn gitstart line 7:user=$(cat ~/.config/gh/hosts.yml | yq r - '"github.com".user')           ^--------------------^ SC2002: Useless cat. Consider 'cmd < file | ..' or 'cmd file | ..' instead.In gitstart line 9:repo=$(basename $dir) # bin                ^--^ SC2086: Double quote to prevent globbing and word splitting.Did you mean:repo=$(basename "$dir") # binIn gitstart line 24:    gh repo create $repo                   ^---^ SC2086: Double quote to prevent globbing and word splitting.Did you mean:    gh repo create "$repo"In gitstart line 25:    git remote add origin git@github.com:$user/$repo.git                                         ^---^ SC2086: Double quote to prevent globbing and word splitting.                                               ^---^ SC2086: Double quote to prevent globbing and word splitting.Did you mean:    git remote add origin git@github.com:"$user"/"$repo".gitFor more information:  https://www.shellcheck.net/wiki/SC2086 -- Double quote to prevent globbing ...  https://www.shellcheck.net/wiki/SC2002 -- Useless cat. Consider 'cmd < file...
It printouts suggestions on how to fix the problem.
I hope this is useful for your next project and speeds up your workflow. With the help of GitHub CLI and yq, we are able to automate a dry repetition. Bash scripts are a great tool to automate your workflow.
Get full access to every story on Medium by becoming a member.