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:
| Method | Remote URL looks like | How Git proves who you are |
|---|---|---|
| SSH | git@github.com:USERNAME/REPOSITORY.git | A key pair stored on your computer |
| HTTPS | https://github.com/USERNAME/REPOSITORY.git | A 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.
-
Check for an existing key:
ls -al ~/.sshFiles such as
id_ed25519andid_ed25519.pubmean you already have a key and can skip to step 4. "No such file or directory" means you have none yet. -
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-keygenthen asks for a passphrase twice: a passphrase protects the key if someone copies the file, or you can press Enter for none. -
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.
-
Add the public key to GitHub. Show it and copy the whole line:
cat ~/.ssh/id_ed25519.pubIt is one line starting with
ssh-ed25519and ending with your label. Copy only this.pubfile'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. -
Test the connection:
ssh -T git@github.comThe first time, SSH warns that the authenticity of
github.comcan't be established and shows a fingerprint. Check it against the fingerprints GitHub publishes, then typeyes. Success looks likeHi 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
sudoor as an administrator, which uses a different user's keys. - The remote URL uses a user other than
git, as ingit@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.
-
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 -
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-practiceand choose Private. Leave the options to add a README, a.gitignorefile and a license unselected: your local repository already has history, and starting files on GitHub would conflict with it. Click Create repository. -
Connect your local repository to it. Replace
USERNAMEwith your GitHub username:git remote add origin git@github.com:USERNAME/git-practice.git git remote -vgit remote -vlistsoriginwith that URL for fetch and push. If you use HTTPS, the URL ishttps://github.com/USERNAME/git-practice.gitinstead. -
Push both branches, setting their upstreams:
git push -u origin main git push -u origin notesEach push reports
* [new branch], as with the shared folder. -
Refresh the repository page on GitHub. It shows
README.md, and the branch menu listsmainandnotes.
Success: the hosted repository shows your commits on main and the
notes branch.
If something differs:
| What you see | Likely cause | Next step |
|---|---|---|
Permission denied (publickey) | The key is not added to your account or not loaded | Run ssh -T git@github.com; check the key steps above |
Could not read from remote repository and correct access rights | A wrong URL or username, or no access | Check git remote -v against the URL GitHub shows |
remote origin already exists | A remote was added before | git remote set-url origin <url> to change it |
Authentication failed over HTTPS | A password was used instead of a token | Use a personal access token or a credential helper |
| The push is refused because of a secret | Push protection detected a secret | Remove it from the commit and revoke the secret |
-
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
git pushprintsgit@github.com: Permission denied (publickey).What is the most likely cause, and how do you check?- 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
- 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.comtests the connection on its own, andssh -vT git@github.comshows which keys were tried. - 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.