Connecting Git to GitHub
Once you have a local project and a GitHub repository, you need to link them together. This "handshake" allows you to share your work with the world.
1. Adding a Remote
A "remote" is a URL that tells Git where your cloud repository is located. By convention, the primary remote is named origin.
# Link your local repo to GitHub
git remote add origin https://github.com/username/repository-name.git2. Your First Push
The push command sends your local snapshots to the cloud. The first time you push, you use the -u flag to set the "upstream" tracking.
# Push your local 'main' branch to 'origin'
git push -u origin main3. SSH vs. HTTPS
How you authenticate with GitHub matters for security and convenience.
HTTPS (Easiest for Beginners)
- URL Format:
https://github.com/user/repo.git - Auth: Requires your GitHub username and a Personal Access Token (PAT). GitHub no longer accepts account passwords for CLI operations.
SSH (Best for Professionals)
- URL Format:
git@github.com:user/repo.git - Auth: Uses a secure "key pair" locally. No password prompt is required once set up.
The Handshake Workflow
[!IMPORTANT] Authentication Error? If you get a "Support for password authentication was removed" error, you must generate a Personal Access Token in your GitHub settings (Settings > Developer settings > Personal access tokens) and use that in place of your password.