Connecting Local Repositories to GitHub
Now that you understand both local Git repositories and GitHub's role as a remote hosting service, it's time to bridge the gap between them. In this lesson, you'll learn how to connect your local Git repositories to GitHub, enabling you to back up your code, collaborate with others, and contribute to open-source projects.
Learning Goals:
- Create a new repository on GitHub
- Connect local repositories to GitHub remotes
- Push local commits to GitHub
- Verify remote connections
- Understand the difference between HTTPS and SSH authentication
Creating a GitHub Repository
Before connecting your local repository, you need a destination on GitHub. Let's create a new repository through GitHub's web interface.
Make sure you're logged into your GitHub account before proceeding. If you don't have an account, create one at github.com (it's free!).
- Click the "+" icon in the top-right corner and select "New repository"
- Name your repository (e.g., "my-first-project")
- Add an optional description
- Choose between public (visible to everyone) or private (only you and collaborators)
- Do not initialize with README, .gitignore, or license - we'll add these from our existing local repository
- Click "Create repository"
Connecting Your Local Repository
After creating the empty repository on GitHub, you'll see instructions for connecting existing repositories. Let's explore the two main authentication methods.
Method 1: HTTPS Connection
HTTPS is the simpler method to get started, though it may require frequent authentication.
# Add the remote origin
git remote add origin https://github.com/your-username/your-repository-name.git
# Verify the remote was added
git remote -v
Method 2: SSH Connection
SSH provides more secure, password-less authentication after initial setup.
# Add the remote origin with SSH
git remote add origin git@github.com:your-username/your-repository-name.git
# Verify the remote was added
git remote -v
- HTTPS Method
- SSH Method
git remote add origin https://github.com/your-username/my-first-project.git
git remote -v
Output:
origin https://github.com/your-username/my-first-project.git (fetch)
origin https://github.com/your-username/my-first-project.git (push)
git remote add origin git@github.com:your-username/my-first-project.git
git remote -v
Output:
origin git@github.com:your-username/my-first-project.git (fetch)
origin git@github.com:your-username/my-first-project.git (push)
Replace your-username with your actual GitHub username and your-repository-name with your repository's name. The origin is a conventional name for the primary remote repository.
Pushing Your Code to GitHub
Now that your remote is configured, it's time to push your local commits to GitHub.
# First, push your main branch and set upstream
git push -u origin main
# For subsequent pushes, you can simply use:
git push
Let's see a complete workflow example:
# Check your current status
git status
# View your commit history
git log --oneline
# Push to GitHub
git push -u origin main
Expected output:
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 8 threads
Compressing objects: 100% (10/10), done.
Writing objects: 100% (15/15), 1.45 KiB | 1.45 MiB/s, done.
Total 15 (delta 2), reused 0 (delta 0), pack-reused 0
remote: Resolving deltas: 100% (2/2), done.
To https://github.com/your-username/my-first-project.git
* [new branch] main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
Verifying the Connection
After pushing, verify everything worked correctly:
# Check remote details
git remote show origin
# View all branches (local and remote)
git branch -a
Sample output:
* main
remotes/origin/main
You can also visit your repository on GitHub in a web browser to confirm your files and commit history are visible.
Handling Authentication
HTTPS Authentication
When using HTTPS, GitHub may prompt for credentials:
# Use personal access token (recommended)
# Or use GitHub CLI for seamless authentication
gh auth login
# For temporary caching of credentials
git config --global credential.helper cache
SSH Setup
For SSH, you'll need to generate and add an SSH key:
# Generate a new SSH key
ssh-keygen -t ed25519 -C "your-email@example.com"
# Add to SSH agent
ssh-add ~/.ssh/id_ed25519
# Copy public key to clipboard
cat ~/.ssh/id_ed25519.pub
# Add the public key to GitHub Settings → SSH and GPG keys
Common Pitfalls
- Wrong remote URL: Double-check your GitHub username and repository name in the URL
- Authentication issues: Ensure your personal access token (HTTPS) or SSH key is properly configured
- Pushing to wrong branch: Use
git branchto verify your current branch before pushing - No commits to push: Make sure you've committed your changes locally before pushing
- Permission denied: Verify you have write access to the repository (especially when contributing to others' repos)
- Upstream not set: The
-uflag ingit push -u origin mainsets the upstream branch for future pushes
Summary
You've successfully learned how to connect local Git repositories to GitHub! Key takeaways:
- Create empty repositories on GitHub for existing local projects
- Use
git remote addto connect local and remote repositories - Choose between HTTPS (easier setup) and SSH (more secure) authentication
- Push commits using
git push -u origin main(first time) andgit push(subsequent) - Verify connections with
git remote -vandgit branch -a
This connection enables collaboration, backup, and opens doors to the wider GitHub ecosystem.
Quiz
Connecting to Remote Repositories – Quick Check
What command adds a remote repository named "origin" with an HTTPS URL?