Git Remote Explained — Adding and Pushing to GitHub

Now, let’s move on to something very practical — pushing your local repository to GitHub.


Local Setup

I created a new folder:

mkdir git-remote-demo-youtube
cd git-remote-demo-youtube
git init

Then added a file:

echo "hello" > hello.txt
git add hello.txt
git commit -m "first commit"

At this point, our repo has only one commit. Let’s confirm:

git log --oneline
e91a2c4 (HEAD -> main) first commit

What is a Remote?

A remote is simply a link to another repository (usually hosted on GitHub, GitLab, or Bitbucket). It allows you and your teammates to push, pull, and collaborate.

To check remotes for your project:

git remote

👉 Initially, there are none.


Creating a Remote on GitHub

  1. Login to GitHub.
  2. Click New Repository.
  3. Name it (I used the same as my local folder).
  4. Keep it Public for now.
  5. Click Create Repository.

GitHub provides two URLs:

  • HTTPS URL
  • SSH URL

⚡ I prefer SSH for secure key-based communication.


Adding a Remote

Copy the SSH URL and add it:

git remote add origin git@github.com:your-username/git-remote-demo-youtube.git

Check remotes:

git remote -v
origin  git@github.com:your-username/git-remote-demo-youtube.git (fetch)
origin  git@github.com:your-username/git-remote-demo-youtube.git (push)

👉 origin is just a name. You can have multiple remotes:

git remote add test git@example.com:dummy.git
git remote -v

Now we have both origin and test.

📌 In real-world projects, you may keep:

  • origin → The official repo
  • upstream → The original open-source project repo
  • your-fork → Your own fork

Pushing to GitHub

Now let’s push:

git push origin main

This means:

  • Push the local branch main
  • To the remote origin
  • Into the remote branch also called main

What happens?

If permissions are not set up, GitHub will reject the push:

ERROR: Permission denied (publickey).
fatal: Could not read from remote repository.

👉 Why? Because GitHub doesn’t know you’re the owner yet. We need to authenticate — either with SSH keys or HTTPS credentials.

⚡ We’ll fix this in the next blog, where I’ll show how to set up SSH keys and authenticate with GitHub.


Summary

  • git remote add <name> <url> → Adds a remote.
  • git remote -v → Shows remotes.
  • You can have multiple remotes (origin, upstream, test).
  • git push origin main → Pushes code to GitHub.
  • Authentication is required to successfully push.

Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉