Connecting to GitHub
18.1 What "Connecting to GitHub" Means
When you connect to GitHub, you:
• Create a remote repository on GitHub
• Link it to your local Git repo using a remote URL
• Push and pull changes between local and remote
GitHub becomes a remote, not a replacement for Git.
• Create a remote repository on GitHub
• Link it to your local Git repo using a remote URL
• Push and pull changes between local and remote
GitHub becomes a remote, not a replacement for Git.
18.2 Creating a Repository on GitHub
On GitHub:
1. Click New Repository
2. Choose a name
3. Do not initialize with README (if repo already exists locally)
4. Copy the repository URL (HTTPS or SSH)
This URL is used to connect your local repo.
1. Click New Repository
2. Choose a name
3. Do not initialize with README (if repo already exists locally)
4. Copy the repository URL (HTTPS or SSH)
This URL is used to connect your local repo.
18.3 Adding a Remote to a Local Repository
Inside your local Git repository:
Example:
origin is the default remote name (convention, not mandatory).
Verify:
1git remote add origin <repository-url>Example:
1git remote add origin https://github.com/username/project-name.gitorigin is the default remote name (convention, not mandatory).
Verify:
1git remote -v18.4 HTTPS vs SSH Authentication
HTTPS
• Easier for beginners
• Requires GitHub Personal Access Token (not password)
Example:
SSH
• More secure
• No repeated authentication after setup
Example:
Most professional setups prefer SSH.
• Easier for beginners
• Requires GitHub Personal Access Token (not password)
Example:
1https://github.com/username/repo.gitSSH
• More secure
• No repeated authentication after setup
Example:
1git@github.com:username/repo.gitMost professional setups prefer SSH.
18.5 Pushing Code for the First Time
After adding the remote:
• -u sets upstream tracking
• Future pushes can use just git push
1git branch -M main
2git push -u origin main• -u sets upstream tracking
• Future pushes can use just git push
18.6 Connecting an Existing GitHub Repo to Local
If the repo already exists on GitHub:
This:• Downloads the full history
• Automatically sets the remote
• Sets up tracking branches
1git clone <repository-url>This:• Downloads the full history
• Automatically sets the remote
• Sets up tracking branches
18.7 Common Issues & Fixes
Remote Already Exists
Wrong Remote URL
Permission Denied (SSH)
• Ensure SSH key is added to GitHub
• Test with:
1git remote remove origin
2git remote add origin <new-url>Wrong Remote URL
1git remote set-url origin <correct-url>Permission Denied (SSH)
• Ensure SSH key is added to GitHub
• Test with:
1ssh -T git@github.com18.8 Key Takeaways
• Connecting to GitHub links your local repo to a remote repository.
• Use git remote add origin <url> to establish the connection.
• HTTPS is simpler; SSH is preferred for long-term use.
• git push -u origin main sets upstream for easy future pushes.
• git clone automatically connects and configures a remote.
• Use git remote add origin <url> to establish the connection.
• HTTPS is simpler; SSH is preferred for long-term use.
• git push -u origin main sets upstream for easy future pushes.
• git clone automatically connects and configures a remote.