Home Coding & Scripting Connecting Local Git Repositories to GitHub

Connecting Local Git Repositories to GitHub

261
0

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:

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 connection
  • origin → 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 simpler
  • main → 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 main branch to the GitHub repo whose nickname is origin
(which points to https://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:

  1. Copy your GitHub repository URL
  2. Add GitHub as a remote called origin
  3. Verify the connection
  4. Push your local code to GitHub

This is the foundation of working with Git and GitHub in real-world projects.

Previous articleSetting Up a GitHub Account & Creating Your First Repository
Next articlePublic vs Private Repositories on GitHub
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here