So far, you've been working with Git entirely on your local machine. Now it's time to connect to GitHub and start collaborating! In this lesson, you'll learn what remote repositories are, how to link your local Git repositories to GitHub, set up authentication (SSH keys or HTTPS), and understand the relationship between local and remote repositories. By the end, you'll be ready to share your code with the world and collaborate with other developers.
What Are Remote Repositories?
A remote repository is a Git repository hosted on a server somewhere—typically GitHub, GitLab, or Bitbucket. It's essentially a copy of your repository that lives in the cloud.
Local vs Remote
Local Repository:
- Lives on your computer
- Only you can access it
- Where you do your actual work
- Fast—no internet required
Remote Repository:
- Lives on a server (like GitHub)
- Others can access it (if you give permission)
- Acts as a backup and collaboration hub
- Requires internet connection
Why Use Remote Repositories?
- Backup: Your code is safe even if your computer breaks
- Collaboration: Multiple people can work on the same project
- Sharing: Show your work to others or contribute to open source
- Deployment: Many services deploy directly from GitHub
- Portfolio: Your GitHub profile showcases your projects
- Access anywhere: Work from different computers
☁️ Think of It Like Cloud Storage
Remote repositories are similar to Google Drive or Dropbox for your code, but with superpowers—you can sync changes, track who made what changes, review code, and more!
Before You Begin
Make sure you have:
- A GitHub account: If you don't have one, create it at github.com
- Git installed and configured: With your name and email (we did this in lesson 3)
- A local repository: We'll use one you created in earlier lessons
# Verify Git is configured
git config --global user.name
git config --global user.email
# Should show your name and emailCreating a Repository on GitHub
Let's create a repository on GitHub to connect to your local repository.
Step 1: Go to GitHub
Navigate to github.com and log in.
Step 2: Create New Repository
- Click the "+" icon in the top-right corner
- Select "New repository"
Step 3: Configure Repository
Fill in the details:
- Repository name: Use the same name as your local repository (e.g.,
my-first-repo) - Description: Optional but helpful (e.g., "My first Git repository")
- Public or Private:
- Public: Anyone can see it (good for open source, portfolios)
- Private: Only you and collaborators you invite can see it
- Initialize repository: Leave all checkboxes UNCHECKED (no README, no .gitignore, no license)
Important: Don't initialize the repository with any files! We're connecting an existing local repository, so we want GitHub to create an empty repository.
Step 4: Create Repository
Click "Create repository" button. You'll see a page with setup instructions—we'll use these next!
Authentication Methods
To connect to GitHub, you need to authenticate. There are two main methods:
Method 1: HTTPS (Simpler, Recommended for Beginners)
Pros:
- Easy to set up
- Works everywhere (even behind firewalls)
- No additional configuration needed
Cons:
- Need to enter credentials (though Git can remember them with credential helper)
- Uses Personal Access Token instead of password
Method 2: SSH (More Convenient Long-term)
Pros:
- No need to enter credentials after setup
- More secure with public-key cryptography
- Preferred by many developers
Cons:
- Requires initial key generation and setup
- May not work in some corporate networks
- Slightly more complex for beginners
Which should you choose?
Start with HTTPS if you're new—it's simpler. You can always switch to SSH later once you're comfortable with the basics.
We'll cover both methods so you can choose what works best for you!
Method 1: HTTPS Authentication
Step 1: Create a Personal Access Token
GitHub no longer accepts passwords for Git operations. You need a Personal Access Token (PAT) instead.
- Go to GitHub Settings (click your profile photo → Settings)
- Scroll down to "Developer settings" (bottom left)
- Click "Personal access tokens" → "Tokens (classic)"
- Click "Generate new token" → "Generate new token (classic)"
- Give it a descriptive note (e.g., "My Laptop Git Access")
- Select expiration (recommend 90 days for learning, or custom)
- Select scopes:
- ✅ repo (full control of private repositories)
- ✅ workflow (if you'll use GitHub Actions)
- Click "Generate token"
- IMPORTANT: Copy the token immediately! You won't be able to see it again.
Save your token securely!
Treat it like a password. Store it in a password manager. You'll need it when Git asks for a password.
Step 2: Add Remote with HTTPS URL
In your local repository, add the GitHub remote:
# Navigate to your repository
cd my-first-repo
# Add the remote (replace USERNAME and REPO with yours)
git remote add origin https://github.com/USERNAME/REPO.git
# Example:
git remote add origin https://github.com/sarahjohnson/my-first-repo.gitStep 3: Verify Remote
git remote -vYou should see:
origin https://github.com/USERNAME/REPO.git (fetch)
origin https://github.com/USERNAME/REPO.git (push)Using Your Token
When you push for the first time, Git will ask for credentials:
- Username: Your GitHub username
- Password: Paste your Personal Access Token (not your GitHub password!)
💾 Credential Storage
Git can remember your credentials so you don't have to enter them every time. Your credential helper (set up in lesson 3) should store your token securely after the first use.
Method 2: SSH Authentication
Step 1: Check for Existing SSH Keys
# Check if you already have SSH keys
ls -al ~/.sshLook for files named id_rsa.pub, id_ed25519.pub, or similar. If you see them, you already have SSH keys!
Step 2: Generate SSH Keys (if needed)
# Generate new SSH key (use your GitHub email)
ssh-keygen -t ed25519 -C "your.email@example.com"
# If your system doesn't support ed25519:
ssh-keygen -t rsa -b 4096 -C "your.email@example.com"When prompted:
- File location: Press Enter to use default
- Passphrase: Optional but recommended for security
Step 3: Add SSH Key to SSH Agent
On macOS/Linux:
# Start the ssh-agent
eval "$(ssh-agent -s)"
# Add your key
ssh-add ~/.ssh/id_ed25519On Windows (Git Bash):
# Start the ssh-agent
eval `ssh-agent -s`
# Add your key
ssh-add ~/.ssh/id_ed25519Step 4: Copy Your Public Key
On macOS:
# Copy to clipboard
pbcopy < ~/.ssh/id_ed25519.pubOn Linux:
# Display key (then copy manually)
cat ~/.ssh/id_ed25519.pub
# Or use xclip if installed
xclip -selection clipboard < ~/.ssh/id_ed25519.pubOn Windows:
# Display key (then copy manually)
cat ~/.ssh/id_ed25519.pub
# Or copy to clipboard
clip < ~/.ssh/id_ed25519.pubStep 5: Add SSH Key to GitHub
- Go to GitHub Settings (click your profile photo → Settings)
- Click "SSH and GPG keys" in the sidebar
- Click "New SSH key"
- Give it a descriptive title (e.g., "My Laptop")
- Paste your public key in the "Key" field
- Click "Add SSH key"
- Confirm with your GitHub password if prompted
Step 6: Test SSH Connection
ssh -T git@github.comYou should see:
Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.If you see this message, SSH is working! Don't worry about the "does not provide shell access" part—that's normal.
Step 7: Add Remote with SSH URL
# Navigate to your repository
cd my-first-repo
# Add the remote (replace USERNAME and REPO with yours)
git remote add origin git@github.com:USERNAME/REPO.git
# Example:
git remote add origin git@github.com:sarahjohnson/my-first-repo.gitStep 8: Verify Remote
git remote -vYou should see:
origin git@github.com:USERNAME/REPO.git (fetch)
origin git@github.com:USERNAME/REPO.git (push)Working with Remotes
Viewing Remotes
# List remotes (short)
git remote
# List remotes with URLs
git remote -v
# Show detailed info about a remote
git remote show originAdding Remotes
# Add a remote
git remote add <name> <url>
# Example with HTTPS
git remote add origin https://github.com/user/repo.git
# Example with SSH
git remote add origin git@github.com:user/repo.git
# You can have multiple remotes with different names
git remote add upstream https://github.com/original/repo.gitRenaming Remotes
# Rename a remote
git remote rename old-name new-name
# Example: rename origin to github
git remote rename origin githubChanging Remote URL
# Change remote URL (useful when switching between HTTPS and SSH)
git remote set-url origin new-url
# Switch from HTTPS to SSH
git remote set-url origin git@github.com:user/repo.git
# Switch from SSH to HTTPS
git remote set-url origin https://github.com/user/repo.gitRemoving Remotes
# Remove a remote
git remote remove <name>
# Example
git remote remove origin🏷️ About Remote Names
origin is just a convention—it's the default name for your primary remote. You can name it anything:
origin- Primary remote (conventional)upstream- Original repo (when you've forked)github,gitlab,bitbucket- Platform-specificproduction,staging- Environment-specific
Your First Push to GitHub
Now that your remote is configured, let's push your code to GitHub!
Step 1: Check Your Branch Name
git branchMake sure you're on main (or master).
Step 2: Push to GitHub
# Push and set upstream
git push -u origin main
# If your branch is named 'master':
git push -u origin masterThe -u flag (short for --set-upstream) tells Git to remember that main should push to origin/main. After this first push, you can just use git push.
What Happens During Push
You'll see output like:
Enumerating objects: 12, done.
Counting objects: 100% (12/12), done.
Delta compression using up to 8 threads
Compressing objects: 100% (8/8), done.
Writing objects: 100% (12/12), 1.23 KiB | 1.23 MiB/s, done.
Total 12 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), done.
To github.com:username/my-first-repo.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.Congratulations! Your code is now on GitHub! Go to your repository URL in a browser to see it:
https://github.com/YOUR-USERNAME/YOUR-REPO
Understanding Upstream Tracking
When you use git push -u origin main, you're setting up an upstream tracking relationship.
What Does This Mean?
Your local main branch now "tracks" the remote origin/main branch. This means:
- Git knows where to push when you run
git push - Git knows where to pull from when you run
git pull - Git can tell you if your branch is ahead or behind the remote
Checking Tracking Info
# See tracking branches
git branch -vv* main a1b2c3d [origin/main] Latest commit messageThe [origin/main] shows this branch tracks origin/main.
Practice Remote Commands
Try these commands to practice working with remotes:
Practice Git Remote Commands
Experiment with viewing and managing remotes
Try these examples:
Troubleshooting Common Issues
Issue: Remote Already Exists
Error: "remote origin already exists"
Solution:
# Option 1: Remove and re-add
git remote remove origin
git remote add origin <url>
# Option 2: Update URL
git remote set-url origin <new-url>Issue: Permission Denied (SSH)
Error: "Permission denied (publickey)"
Solutions:
- Check SSH key is added to ssh-agent
- Verify public key is on GitHub
- Test SSH connection:
ssh -T git@github.com - Try using HTTPS instead if SSH continues to fail
Issue: Authentication Failed (HTTPS)
Error: "Authentication failed"
Solutions:
- Make sure you're using a Personal Access Token, not your password
- Check token has correct permissions (repo scope)
- Verify token hasn't expired
- Clear credential cache and try again
Issue: Push Rejected
Error: "Updates were rejected because the remote contains work that you do not have locally"
Solution: Pull first, then push:
git pull origin main
git push origin mainBest Practices
1. Use SSH for Convenience
Once set up, SSH doesn't require entering credentials. Great for daily use.
2. Keep Tokens Secure
Never commit Personal Access Tokens to your repository! Store them in a password manager.
3. Use Descriptive Remote Names
If you have multiple remotes, use clear names:
git remote add origin git@github.com:you/your-repo.git
git remote add upstream git@github.com:original/repo.git
git remote add production https://deploy.service.com/repo.git4. Set Upstream on First Push
Always use -u on your first push to set up tracking:
git push -u origin main5. Verify Before Pushing
# Check what you're about to push
git log origin/main..HEAD
# Check remote is correct
git remote -vCommand Reference
Here's a quick reference of remote commands:
# Viewing Remotes
git remote # List remote names
git remote -v # List remotes with URLs
git remote show <name> # Detailed remote info
# Adding Remotes
git remote add <name> <url> # Add new remote
# Changing Remotes
git remote rename <old> <new> # Rename remote
git remote set-url <name> <url> # Change URL
git remote remove <name> # Remove remote
# Pushing
git push -u origin main # Push and set upstream
git push # Push to tracked remote
git push origin main # Push to specific remote/branch
# SSH Key Management
ssh-keygen -t ed25519 -C "email" # Generate SSH key
ssh-add ~/.ssh/id_ed25519 # Add key to agent
ssh -T git@github.com # Test connectionKey Takeaways
- Remote repositories are Git repositories hosted on servers like GitHub
originis the conventional name for your primary remote- HTTPS uses Personal Access Tokens; SSH uses cryptographic keys
git remote addconnects your local repo to GitHubgit push -u origin mainpushes code and sets up tracking- SSH is more convenient after initial setup; HTTPS is simpler to start
- Never use your GitHub password for Git—use tokens or SSH keys
- You can have multiple remotes with different names
- Upstream tracking makes
git pushandgit pullwork without specifying the remote - Always verify your remote configuration before pushing
What's Next?
Excellent work! You've successfully connected your local Git repository to GitHub and pushed your code. Your projects are now backed up in the cloud and ready to share with the world.
In the next lesson, we'll dive deeper into pushing and pulling code. You'll learn how to keep your local and remote repositories synchronized, handle updates from GitHub, work with different branches on remotes, and understand the complete workflow of collaborative development. This is where Git really becomes powerful for teamwork!
🎯 Practice Assignment
Before the next lesson:
- Create 2-3 repositories on GitHub
- Connect them to local repositories with
git remote add - Push your code to GitHub with
git push -u origin main - Visit your repositories on GitHub to verify they're there
- Try both HTTPS and SSH (if you're comfortable)
- Practice viewing remote info with
git remote -vandgit remote show origin