How to set up a new GitHub repository using command line without browser access?

I just finished setting up a local Git repository on my machine:

~$ mkdir myproject
~$ cd myproject
~$ git init
~$ touch README.md
~$ git add README.md
~$ git commit -m 'initial setup'

Can I use terminal commands to establish a new remote repository on GitHub and upload my changes? I understand that opening a web browser and navigating to GitHub’s repository creation page is straightforward, but I’m curious if there’s a command line approach to accomplish this task.

I’ve searched through multiple tutorials and guides, but haven’t found clear instructions on creating remote repositories directly through CLI commands. Most resources I’ve encountered focus on connecting to existing remote repositories rather than creating new ones from scratch using terminal commands.

I struggled with this exact same issue when I started working on headless servers without GUI access. What worked for me was combining environment variables with the GitHub API approach. Set up your personal access token as an environment variable in your shell profile with export GITHUB_TOKEN=your_token_here so you don’t have to type it repeatedly. Then you can create a simple bash function that combines the repository creation and remote setup in one command. Something like create_github_repo() { curl -H "Authorization: token $GITHUB_TOKEN" -d '{"name":"'$1'"}' https://api.github.com/user/repos && git remote add origin https://github.com/yourusername/$1.git; }. This way you just run create_github_repo myproject and it handles both steps automatically. I found this approach more reliable than GitHub CLI on older systems where package management was restricted.

honestly the easiest way ive found is just using git remote add origin after making the repo on github manually. yeah i know you wanted pure cli but sometimes the web ui is just faster for one-time setup. once you got the remote added, standard git push -u origin main works perfectly. saves time instead of messing with tokens and api calls

Since you already have your local repository initialized, you can use SSH with GitHub’s API for a more streamlined approach. First generate an SSH key if you haven’t already with ssh-keygen -t rsa -b 4096 -C "[email protected]" and add it to your GitHub account through settings. Then use the API endpoint with your SSH credentials to create the repository programmatically. The advantage here is that SSH authentication persists across sessions without needing to manage tokens. After creating the remote repository via API, configure your remote with git remote add origin [email protected]:yourusername/myproject.git and push using git push -u origin main. This method works well when you’re already using SSH for other GitHub operations and want to maintain consistency in your authentication approach.

You’ll need to install GitHub CLI first with gh auth login to authenticate your account. Once that’s done, creating repositories from terminal becomes straightforward. From your project directory, run gh repo create myproject --public (or --private if you prefer). This creates the remote repository and automatically adds it as origin to your local repo. Then just git push -u origin main to upload your commits. I’ve been using this workflow for months now and it’s much faster than switching to browser every time I start a new project. The GitHub CLI tool handles all the API calls behind the scenes, so you get the same result as creating through the web interface but without leaving your terminal.