After creating commits locally, the next step is connecting the repository to GitHub.
Two paths to connecting
You can connect a local repository to GitHub in two ways.
The easiest path uses the GitHub CLI (gh), which handles authentication, SSH key setup, and remote configuration in a single interactive command. If you prefer an all-in-one tool, jump to the GitHub CLI section below.
The manual path uses plain Git commands and gives you full control over each step. That path comes first.
Start by creating the repository on GitHub
Create a new repository through the GitHub web UI. This gives your local repository a remote destination to push to.
Note the repository name and the remote URL GitHub provides — you will need them in a moment.
git remote add origin connects the local repository to the remote
To add the GitHub repository as your main remote:
git remote add origin https://github.com/OWNER/REPO.git
This does not upload your commits yet. It only records the remote connection in your local Git configuration.
SSH vs HTTPS URLs
GitHub supports two authentication methods:
- HTTPS (
https://github.com/OWNER/REPO.git) — works everywhere but may require entering credentials each time unless you have a credential helper configured - SSH (
git@github.com:OWNER/REPO.git) — uses SSH keys for authentication and is recommended for regular contributors
To set up SSH authentication manually:
- Generate an SSH key if you do not have one:
ssh-keygen -t ed25519 -C "you@example.com" - Add the public key to your GitHub account under Settings > SSH and GPG keys
- Use the SSH URL when connecting:
git remote add origin git@github.com:OWNER/REPO.git
SSH is the recommended method for regular use because it avoids repeated credential prompts.
An easier way: GitHub CLI
The GitHub CLI (gh) is an official command-line tool that handles authentication, SSH key setup, and remote configuration in a single interactive command. If you prefer not to manage SSH keys and Git remotes manually, gh does most of it for you.
Installing GitHub CLI
On macOS, install with Homebrew:
brew install gh
On other platforms, see the GitHub CLI installation guide.
Authenticating with gh auth login
Run:
gh auth login
This starts an interactive flow that asks you to choose a protocol — SSH or HTTPS. If you choose SSH, it detects any existing SSH keys and offers to generate and upload a new one if none exist. It then opens your browser to authorize your GitHub account and configures Git to use the chosen protocol automatically.
After gh auth login completes, your account is authenticated, your SSH key is configured, and Git is ready for pushes and pulls — all from one command.
Connecting a repository with gh repo create
If you have a local repository and want to create the GitHub remote and push in one step:
gh repo create
This interactively asks for the repository name, visibility, and whether to push an existing local repository. It creates the remote, pushes your commits, and even offers to open the repository in your browser.
If you prefer the manual approach, the rest of this lesson covers how to connect and push without gh.
git push -u origin <branch> sends the local branch to GitHub
A common first push looks like this:
git push -u origin main
Here:
originis the remote namemainis the branch being pushed-utells Git to remember the upstream tracking relationship
This does two things:
- pushes the local
mainbranch to theoriginremote - sets the upstream tracking relationship for that branch
That upstream link is why later commands can often use shorter forms such as git push and git pull.
A clean first-push flow
- Create the repository on GitHub.
- Copy the repository URL.
- In the local repository, run
git remote add origin …. - Push the current branch with
git push -u origin main.
What changed locally and remotely
After this flow:
- the local repository knows about a remote named
origin - the local branch has been uploaded to GitHub
- GitHub now has the commit history that existed locally
Quick Check
One answerWhat changes after you run git remote add origin ...?
Choose the best answer and use it to track your progress through the lesson.
Why that answer is correct
Adding a remote only stores connection information in the local repo. Uploading commits still requires `git push`.