Git Push — Pushing Local Repository to GitHub with SSH

In the previous blog, we connected our local repository with a remote repository on GitHub using git remote add.
Now, it’s time to actually push our commits to GitHub.


Current Setup

  • Local repo: git-remote-demo-youtube
  • One commit with a file.txt
  • Remote repo already created on GitHub
  • Remote name: origin

Verify remotes:

git remote -v

Output:

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

The Push Command

We use the following syntax:

git push <remote> <local-branch>:<remote-branch>

For our case:

git push origin main:main
  • origin → Remote repository name
  • First main → Local branch
  • Second main → Remote branch

Authentication Error ❌

When running the push command, GitHub denies access:

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

👉 GitHub doesn’t yet know that this machine belongs to you.


Solution: SSH Key Authentication 🔑

To authenticate:

Step 1: Generate SSH Key Pair

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

Press Enter 3–4 times to accept defaults.

This generates:

  • Private Key~/.ssh/id_rsa
  • Public Key~/.ssh/id_rsa.pub

⚠️ Never share your private key.


Step 2: Copy Public Key

cat ~/.ssh/id_rsa.pub

Copy the entire string.


Step 3: Add SSH Key to GitHub

  1. Go to GitHub → Settings → SSH and GPG keys
  2. Click New SSH Key
  3. Give it a title (e.g., Laptop Key)
  4. Paste your public key
  5. Save

Now GitHub trusts your machine.


Retry Push

Run again:

git push origin main:main

🎉 Success! Your commit is pushed to GitHub.


Verification on GitHub

Open your repo on GitHub:

  • File file.txt is visible
  • Commit message first commit is listed
  • Commit ID matches your local log
git log --oneline

Output:

f3cb6f5 first commit

This same commit ID is now on GitHub.


Key Takeaways

  • git push origin main:main → Push local branch main to remote branch main.
  • GitHub requires authentication — use SSH keys for secure access.
  • After adding your SSH key to GitHub, pushes and pulls will work without entering a password.

What’s Next?

In the next blog, we’ll explore how to work with multiple remotes (origin, upstream, fork repos) and why you might need them when contributing to open-source projects.


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! 🎉