Once you have a local Git repository on your computer and a GitHub repository online, the next step is to connect them.
This connection allows us to:
- Upload our local commits to GitHub
- Access our code from anywhere
- Share our projects with others
- Collaborate with our team
In this post, we will walk through this process step-by-step in the simplest possible way.
📌 Prerequisites
Before continuing, make sure we have completed the following:
1. Set up your local Git repository as described in these posts:
- Installing and Setting Up Git on Windows, macOS, and Linux
- Understanding the Git Repository Structure and Workflow Stages
- Understanding Git Branches
- Understanding Merging with Git Branches
2. Create and log into a GitHub account AND create a GitHub repository
Follow the steps in:
Setting Up a GitHub Account & Creating Your First Repository
Once both sides (local repo + GitHub repo) are ready, you can connect them.
🧩 1. What Does “Connecting to GitHub” Mean?
When you connect your local repo to GitHub, you are telling Git:
“This is the online location (remote repository) where I want to store my code.”
After this connection, Git understands:
- Where to push your commits
- Where to pull updates from
- How to sync your local project with GitHub
This connection is done using a remote URL.
🔗 2. Get Your Repository URL from GitHub
Go to your GitHub repository page.
You will see a Code button (usually green).
Click it to display the clone URL.
Choose HTTPS, and copy the URL.
It looks like this:
https://github.com/your-username/your-repo-name.git
This is the address Git will use to communicate with GitHub.

🛠️ 3. Connect Your Local Repo to GitHub
Open your terminal inside your local project folder.
Replace the URL with your actual GitHub repo URL and run:
git remote add origin https://github.com/your-username/your-repo-name.git
✔ What this command does:
remote add→ Adds a remote connectionorigin→ The default name for the GitHub repository<URL>→ The GitHub repo location
You now have a connection between your local repo and GitHub.
🧪 4. Verify the Connection
Run:
git remote -v
You’ll see something like:

This confirms your local and GitHub repos are linked.
🚀 5. Push Your Local Code to GitHub for the First Time
If this is your first time pushing, run:
git push -u origin main
✔ Explanation:
push→ Send your commits to GitHub-u→ Sets “origin” as the default so future pushes are simplermain→ The name of your main branch
After this, you can simply use:
git push
for future updates.
✔️ What does origin mean git push -u origin main?
origin is just a nickname (short name) that your local Git uses for the GitHub repository URL.
You chose this nickname when you ran:
git remote add origin https://github.com/your-username/your-first-repo.git
In that command:
https://github.com/your-username/your-first-repo.git
→ This is the actual GitHub repository (your real repo on GitHub).origin
→ This is the nickname your local Git uses for that URL.
It’s just like saving a phone number in your contacts:
- Phone number = GitHub URL
- Contact name =
origin
So when you later run:
git push origin main
you are really saying:
“Push my
mainbranch to the GitHub repo whose nickname isorigin
(which points tohttps://github.com/your-username/your-first-repo.git).”
Almost everyone uses origin as this nickname, which is why you see it in most Git examples.
When you do this for the first time, Git will initiate authentication.

Click Sign in with a code. Alternatively, if you have just one browser from which you connect to GitHub, you may select Sign in with your browser.
Clicking Sign in with a code will give you following screen:

Go to the given URL from the browser on which you have logged in to the right GitHub account, enter this code and click Continue.

You will see a screen as following. Click on Authorize git-ecosystem.

If you have not verified email before, you may get a prompt to confirm that by pasting a code you received in email. Once done, you should see like following.

⚠️ If You See a Rejection Error on First Push
When you go back to the terminal, you may see an error like follows:

This happens when GitHub has at least one commit (usually created when you added a README or license).
✅ Fix: Pull GitHub Changes First
Run:
git pull origin main --allow-unrelated-histories
This pulls the initial GitHub commit (like README.md) into your local repository.
Git may ask you for a comment and create a merge commit — this is normal.

Then push again:
git push -u origin main
This time, your push will work successfully.

🎉 6. Check Your GitHub Repository
Go back to GitHub and refresh the page. You should now see your files uploaded!

Your local project is now safely stored online.
🙌 Summary
In this post, you learned how to:
- Copy your GitHub repository URL
- Add GitHub as a remote called
origin - Verify the connection
- Push your local code to GitHub
This is the foundation of working with Git and GitHub in real-world projects.