Git and GitHub are two essential tools for modern developers. Git is the version control system that tracks your code changes locally, while GitHub is a cloud-based platform that hosts Git repositories and enables collaboration.
Connecting Git to GitHub allows you to push your local changes to a remote repository, collaborate with others, and back up your work safely.
This guide walks you through the process step-by-step.
🛠 Prerequisites
Before starting, make sure:
- You have Git installed on your machine.
- You have a GitHub account.
- You have created a repository on GitHub or are ready to create one.
Step 1: Install Git (If Not Already Installed)
On Ubuntu/Debian:
sudo apt update
sudo apt install git
On Windows:
Download and install Git from git-scm.com.
Step 2: Configure Git with Your GitHub Account
Set your username and email in Git to match your GitHub account:
git config --global user.name "Your Name"
git config --global user.email "yo********@ex*****.com"
Step 3: Generate SSH Key (Optional but Recommended)
Using SSH keys simplifies authentication without entering your password every time.
Generate an SSH key (if you don’t have one):
ssh-keygen -t ed25519 -C "yo********@ex*****.com"
Start the SSH agent:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
Copy your SSH public key to GitHub:
cat ~/.ssh/id_ed25519.pub
- Go to GitHub → Settings → SSH and GPG keys → New SSH key
- Paste the copied key and save
Step 4: Clone the Repository or Initialize Git Locally
To clone an existing GitHub repo:
git clone gi*@gi****.com:username/repo.git
Or using HTTPS:
git clone https://github.com/username/repo.git
To create a new local Git repo:
mkdir my-project
cd my-project
git init
Step 5: Link Local Repository to GitHub Remote
If you created a new local repo, add GitHub as the remote:
git remote add origin gi*@gi****.com:username/repo.git
Or via HTTPS:
git remote add origin https://github.com/username/repo.git
Step 6: Push Your Code to GitHub
Add files, commit, and push:
git add .
git commit -m "Initial commit"
git push -u origin main
Replace
main
with your branch name if different.
Troubleshooting Tips
- If you face authentication errors, check your SSH keys or try HTTPS.
- Use
git remote -v
to verify remote URLs. - Use
git status
to check current changes and branch info.
Summary Table
Step | Command / Action |
---|---|
Configure Git user info | git config --global user.name "Name" |
Generate SSH keys | ssh-keygen -t ed25519 -C "em***@ex*****.com" |
Clone repo | git clone gi*@gi****.com:user/repo.git |
Initialize local repo | git init |
Add remote | git remote add origin gi*@gi****.com:user/repo.git |
Push code | git push -u origin main |
Final Thoughts
Connecting Git to GitHub unlocks powerful collaboration and version control capabilities. Once set up, you can easily sync your code, manage branches, and contribute to open source or team projects.