Skip to main content

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.

tip

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!).

  1. Click the "+" icon in the top-right corner and select "New repository"
  2. Name your repository (e.g., "my-first-project")
  3. Add an optional description
  4. Choose between public (visible to everyone) or private (only you and collaborators)
  5. Do not initialize with README, .gitignore, or license - we'll add these from our existing local repository
  6. 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.

Connect using HTTPS
# 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.

Connect using SSH
# 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 Setup
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)
note

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.

Push 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:

Complete push workflow
# 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:

Verify remote configuration
# 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:

HTTPS authentication options
# 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:

SSH key setup
# 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 branch to 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 -u flag in git push -u origin main sets 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 add to 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) and git push (subsequent)
  • Verify connections with git remote -v and git 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?

Question 1/5