Connecting a Local Repo to GitHub
You have a local Git repository with commits, and you have an empty GitHub repository waiting. Connecting them is a two-step process: tell your local repo where the remote is, then push your commits there.
What a remote is
Section titled “What a remote is”A remote is a named reference to a repository stored somewhere other than your local machine — usually on GitHub, but it could be any Git-accessible location.
Most repositories have one remote, named origin by convention. The name “origin” is not special — it is just what GitHub’s instructions use, and it has become the standard. You could name it anything.
git remote add origin https://github.com/yourusername/git-practice.gitThis registers the URL as a remote named origin in your local repository configuration. It does not move any files yet — it just saves the address so Git knows where to push and pull.
Verifying your remotes
Section titled “Verifying your remotes”git remote -vOutput:
origin https://github.com/yourusername/git-practice.git (fetch)origin https://github.com/yourusername/git-practice.git (push)Two lines per remote — one for fetching (downloading) and one for pushing (uploading). Usually the same URL.
Setting the upstream and pushing
Section titled “Setting the upstream and pushing”Push your commits to GitHub for the first time:
git push -u origin mainWhat each part means:
git push— send commits to the remote-u— set the upstream: link your localmainbranch toorigin/mainso futuregit pushandgit pullcommands know where to go without specifying the remote and branch name each timeorigin— the remote to push tomain— the local branch to push
After this first push, future pushes from main only need:
git pushGit already knows where to push.
Authentication
Section titled “Authentication”When you push for the first time, GitHub will ask you to authenticate. The method depends on your setup:
HTTPS with a Personal Access Token (PAT):
GitHub no longer accepts passwords over HTTPS. Instead:
- On GitHub: Settings → Developer settings → Personal access tokens → Tokens (classic) → Generate new token
- Give it a name and check the
reposcope - Copy the token — you will only see it once
- When Git prompts for a username, enter your GitHub username. For the password, paste the token.
Most systems (macOS Keychain, Windows Credential Manager, Git Credential Manager) will save the token so you only authenticate once.
SSH:
If you have set up SSH keys for GitHub, push commands authenticate automatically with no token required. SSH setup is a one-time configuration — instructions are in GitHub’s documentation under “Connecting to GitHub with SSH.”
After pushing
Section titled “After pushing”Once the push completes, visit your GitHub repository in a browser. Refresh the page — your commits and files are now there, visible to anyone (if public) or just you (if private).
The branch, commit messages, file contents, and complete history are all visible through GitHub’s web interface.
What happens to the branch pointer
Section titled “What happens to the branch pointer”After pushing, your git log --oneline shows:
b7e9d03 (HEAD -> main, origin/main) Add difficulty notesorigin/main is a remote-tracking branch — a local reference to where origin’s main branch was the last time you communicated with GitHub. As you make more local commits, main will advance ahead of origin/main until you push again.
Exercise
Section titled “Exercise”- In your
git-practicefolder, connect the GitHub repository you created in the previous lesson:
git remote add origin https://github.com/yourusername/git-practice.git(Replace yourusername with your actual GitHub username and use the URL GitHub showed you.)
- Verify the remote was added:
git remote -v- Push to GitHub:
git push -u origin mainAuthenticate if prompted.
-
Visit your GitHub repository in a browser and confirm your commits and files are visible.
-
Run
git log --onelineand observe thatorigin/mainnow appears in the output, pointing to the same commit asmain.
- A remote is a named reference to a repository at another location.
originis the conventional name for a GitHub remote. git remote add origin <url>registers the remote. It does not move any data.git push -u origin mainpushes commits to GitHub and sets the upstream so future pushes only needgit push.- Authentication: HTTPS requires a Personal Access Token (not a GitHub password). SSH authenticates automatically with keys.
origin/mainis a remote-tracking branch — a local reference to where GitHub’smainwas last synced.
Next: pushing and pulling — the commands you use every day to keep your local and remote repositories in sync.