Skip to content

Instantly share code, notes, and snippets.

@gyenabubakar
Last active March 1, 2024 18:09
Show Gist options
  • Save gyenabubakar/2de7ade86a6ddf70165edd98b8586844 to your computer and use it in GitHub Desktop.
Save gyenabubakar/2de7ade86a6ddf70165edd98b8586844 to your computer and use it in GitHub Desktop.
Create a GitHub Repository from a Local Directory on Linux or macOS

Prerequisites

  1. Must have Git installed.
  2. Must have GitHub CLI installed.

How to

  1. Create a file git-create-repo.sh in your home directory and save the content of git-create-repo.sh below in the file.
  2. Add a Git alias called create-repo that runs the script:
git config --global alias.create-repo "! ~/git-create-repo.sh"
  1. Make the file executable:
chmod +x ~/git-create-repo.sh
  1. Use the Git alias inside the directory for which you're creating the GitHub repository, like this:
git create-repo [<repo-name>]

If no repository name is provided, the script will fallback to the directory name.

If you're not already logged into the GitHub CLI, you'll be prompted to do so. From there, create-repo will initialise a new Git repository (if it isn't initialised already), create the repository on GitHub with GitHub CLI, and push an initial commit. Make sure to have .gitignore setup to prevent adding files you don't want exposed to the Git history. It'll then ask you to press Enter or Return to open the repository page on GitHub in your default browser.

#!/bin/bash
# Colours
RED='\033[0;31m'
GREEN='\033[0;32m'
NC='\033[0m' # No Colour
# if current dir is empty (or only has a .git dir), exit with error
if [ -z "$(ls -A)" ]; then
echo "❌ — Current directory is empty. Please run this script from a directory with files."
exit 1
fi
# if gh is not installed, install it
if ! command -v gh &> /dev/null; then
echo "❌ — GitHub CLI is not installed. Please install it from https://cli.github.com/manual/"
exit 1
fi
if ! gh auth status >/dev/null 2>&1; then
echo "🔑 Please login to GitHub CLI using your GitHub credentials"
gh auth login --web --hostname github.com
echo ""
fi
# if current directory is not a Git repository, initialise it
if [ ! -d .git ]; then
echo "⏳ Initialising Git repository..."
git init
fi
remote_origin=$(git config --get remote.origin.url)
# if current directory already has remote origin, exit with error
if [ -n "$remote_origin" ]; then
echo "❌ — Current directory already has a remote origin. Please run this script from a directory without a remote origin."
exit 1
fi
# if there are uncommitted changes, commit them
if [ -n "$(git status --porcelain)" ]; then
git add .
git commit -m "Initial commit"
fi
# Check if a command line argument is provided
if [ "$#" -ge 1 ]; then
repo_name="$1" # Assign the first argument to the variable
else
repo_name=$(basename "$PWD") # Assign the current directory name to the variable
fi
# check if repository name is available
username=$(gh api user -q '.login')
message=$(gh api -q '.message' repos/"$username"/"$repo_name")
if [[ ! $message == '{"message"'* ]]; then
echo -e "❌ — A repository with the name ${RED}$repo_name${NC} already exists."
exit 1
fi
# create a new repository on GitHub
echo "⏳ Creating new repository on GitHub..."
# Check the exit status of this command (0 means success, non-zero means failure)
result=$(gh repo create "$repo_name" --public --source=.)
# shellcheck disable=SC2181
if [ $? -ne 0 ]; then
# Print the error message from the failed command
echo -e "${RED}$result${NC}"
exit 1
fi
git branch -M main
git push --quiet -u origin main
clear
echo -e "🎉🥳 ${GREEN}New repository created on GitHub!${NC}"
echo ""
echo "🔗 Press ENTER to open the repository in your browser, or press Ctrl+C to exit."
read -r
# open the repository in the browser
remote_origin=$(git config --get remote.origin.url)
if command -v open &> /dev/null; then
open "$remote_origin"
elif command -v xdg-open &> /dev/null; then
xdg-open "$remote_origin"
elif command -v gnome-open &> /dev/null; then
gnome-open "$remote_origin"
fi
@gyenabubakar
Copy link
Author

gyenabubakar commented Mar 1, 2024

Okay, I just noticed by having GitHub Desktop, you don't need this script 😒. Just run:

gh repo create my-project --private --source=. --remote=upstream

Due diligence, Gyen. Due diligence! 🤦‍♂️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment