Git Foundations / Optional: Review on a Hosting Service

Optional: connect to a hosting service

Lesson 32 of 33. Open to read. No account needed.

Before You Start

This module is optional. It moves from the shared folder on your computer to a hosting service, a website that stores repositories and adds tools for working together. The examples use GitHub, the most widely used one; other services work in a similar way, with different menus.

You need:

  • A free GitHub account. Creating one is not covered here.
  • A network connection.
  • About 30 minutes for this lesson.

Keep your practice repository private, never put real passwords or keys in it, and delete it when you finish the module. Hosting services change their pages often: if a menu name below differs from what you see, GitHub's own documentation, linked in the references, has the current steps.

Two Ways to Sign In From Git

When Git talks to GitHub, GitHub needs to know it is you. There are two common ways, and each uses its own form of repository URL:

MethodRemote URL looks likeHow Git proves who you are
SSHgit@github.com:USERNAME/REPOSITORY.gitA key pair stored on your computer
HTTPShttps://github.com/USERNAME/REPOSITORY.gitA personal access token, or a credential helper that manages one

Over HTTPS, GitHub no longer accepts your account password for Git: when Git asks for a password, it needs a personal access token instead. A credential helper, such as Git Credential Manager, can handle that sign-in for you and remember it. Either method works; this lesson walks through SSH, because once the key is set up nothing needs to be typed again.

Setting Up an SSH Key

An SSH key comes in two files: a private key, which never leaves your computer, and a public key, which you give to GitHub. When you push, your computer proves it holds the private key that matches.

  1. Check for an existing key:

    ls -al ~/.ssh
    

    Files such as id_ed25519 and id_ed25519.pub mean you already have a key and can skip to step 4. "No such file or directory" means you have none yet.

  2. Create a key, using your GitHub email address as its label:

    ssh-keygen -t ed25519 -C "your_email@example.com"
    

    Press Enter to accept the suggested file location. ssh-keygen then asks for a passphrase twice: a passphrase protects the key if someone copies the file, or you can press Enter for none.

  3. Load the key into the SSH agent, the background program that holds keys for you. The commands differ between Windows, macOS and Linux, so follow the "Adding your SSH key to the ssh-agent" steps for your system in GitHub's SSH documentation.

  4. Add the public key to GitHub. Show it and copy the whole line:

    cat ~/.ssh/id_ed25519.pub
    

    It is one line starting with ssh-ed25519 and ending with your label. Copy only this .pub file's contents; never share the private key. On GitHub, click your profile picture, then Settings. In the "Access" section, click SSH and GPG keys, then New SSH key. Give the key a title such as the name of your computer, keep the type as an authentication key, paste the line into "Key" and click Add SSH key.

  5. Test the connection:

    ssh -T git@github.com
    

    The first time, SSH warns that the authenticity of github.com can't be established and shows a fingerprint. Check it against the fingerprints GitHub publishes, then type yes. Success looks like Hi USERNAME! You've successfully authenticated, but GitHub does not provide shell access.

When Authentication Fails

Permission denied (publickey) means GitHub could not match a key you offered to your account. The usual causes are:

  • The public key was never added to your account, or a different key was.
  • The key is not loaded in the SSH agent.
  • Git or SSH is being run with sudo or as an administrator, which uses a different user's keys.
  • The remote URL uses a user other than git, as in git@github.com:....

ssh -vT git@github.com prints details of which keys SSH tried. Over HTTPS, Authentication failed usually means a password was used where a personal access token is required, or the token has expired.

Hosts can also refuse a push for its content. On GitHub.com, push protection for users is on by default and blocks pushes to public repositories when it detects a secret in them. If that happens, follow the secrets advice from the ignore lesson: revoke the secret first.

Exercise: Push a Practice Repository to a Hosting Service

Starting state: Git configured as in the configuration lesson, a GitHub account, and an SSH key added to it as described above. If you chose HTTPS, use the HTTPS URL in step 3 and sign in with a token or credential helper when asked.

  1. Create a local practice repository with two branches:

    mkdir -p ~/git-practice/hosted-practice
    cd ~/git-practice/hosted-practice
    git init -b main
    cat > README.md <<'EOF'
    # Hosted practice
    
    A private repository for practising with a hosting service.
    EOF
    git add README.md
    git commit -m "Add README"
    git switch -c notes
    cat > notes.md <<'EOF'
    # Notes
    EOF
    git add notes.md
    git commit -m "Start notes"
    git switch main
    git log --oneline --all
    
  2. Create an empty private repository on GitHub. In the upper-right corner of any page, open the + menu and click New repository. Choose your account as the owner, name it git-practice and choose Private. Leave the options to add a README, a .gitignore file and a license unselected: your local repository already has history, and starting files on GitHub would conflict with it. Click Create repository.

  3. Connect your local repository to it. Replace USERNAME with your GitHub username:

    git remote add origin git@github.com:USERNAME/git-practice.git
    git remote -v
    

    git remote -v lists origin with that URL for fetch and push. If you use HTTPS, the URL is https://github.com/USERNAME/git-practice.git instead.

  4. Push both branches, setting their upstreams:

    git push -u origin main
    git push -u origin notes
    

    Each push reports * [new branch], as with the shared folder.

  5. Refresh the repository page on GitHub. It shows README.md, and the branch menu lists main and notes.

Success: the hosted repository shows your commits on main and the notes branch.

If something differs:

What you seeLikely causeNext step
Permission denied (publickey)The key is not added to your account or not loadedRun ssh -T git@github.com; check the key steps above
Could not read from remote repository and correct access rightsA wrong URL or username, or no accessCheck git remote -v against the URL GitHub shows
remote origin already existsA remote was added beforegit remote set-url origin <url> to change it
Authentication failed over HTTPSA password was used instead of a tokenUse a personal access token or a credential helper
The push is refused because of a secretPush protection detected a secretRemove it from the commit and revoke the secret
  1. Keep the hosted repository if you will do the next lesson; it uses it. When you finish the module, or now if you stop here, clean up:

    • Delete the hosted repository: on its page, open Settings, scroll to the "Danger Zone", click Delete this repository and follow the confirmation steps. Deletion is permanent, although GitHub can restore some deleted repositories within 90 days.

    • Remove the remote and the local folder:

      cd ~/git-practice/hosted-practice
      git remote remove origin
      cd ~/git-practice
      ls
      rm -rf hosted-practice
      ls
      
    • If you made a key or token only for this module, delete it from your GitHub settings as well.

Independent task: without changing the hosted repository, switch your local origin to the other URL form (HTTPS if you used SSH, or the other way round), check it with git remote -v, then switch it back.

Check Your Understanding

  1. git push prints git@github.com: Permission denied (publickey). What is the most likely cause, and how do you check?
  2. Why should the new repository on GitHub be created empty, without a README?

Answer and Reasoning

Independent task

git remote set-url origin https://github.com/USERNAME/git-practice.git
git remote -v
git remote set-url origin git@github.com:USERNAME/git-practice.git
git remote -v

git remote set-url changes where origin points without touching any commits. Use the URLs in the opposite order if you started with HTTPS.

Understanding check

  1. GitHub could not match any key your computer offered to your account. The public key may not be added, the key may not be loaded in the agent, or Git may be running with sudo. ssh -T git@github.com tests the connection on its own, and ssh -vT git@github.com shows which keys were tried.
  2. Your local repository already has history. A README created on GitHub would be a separate first commit there, so the two histories would not share a starting point, and your first push would be rejected.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy