Skip to content
0

git-over-ssh

Now you've done some modifications to your local repository, and want to git push to the remote repo on GitHub. How are you going to prove that you is you, or you is the rightful owner of the remote repo? The answer is Git over SSH.

git clone https://github.com/Alowree/marapython-teek.git ~/OneDrive/marapython

I've just cloned my blog from a remote repository on GitHub to my local hard drive on Arch Linux, done some editing, and tried to git push it back to the remote repo. However, I received the following error:

╭╴  alowree on Arch Linux via  main at ~/OneDrive/marapython took  9s via   26.2.0
╰─❯ git status
On branch main
Your branch is ahead of 'origin/main' by 1 commit.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean

╭╴  alowree on Arch Linux via  main at ~/OneDrive/marapython via   26.2.0
╰─❯ git push
Username for 'https://github.com': alowree@hotmail.com
Password for 'https://alowree%40hotmail.com@github.com':
remote: Invalid username or token. Password authentication is not supported for Git operations.
fatal: Authentication failed for 'https://github.com/Alowree/marapython-teek.git/'

What should I do next?

The error message tells you exactly what's wrong: "Password authentication is not supported for Git operations." Since August 2021, GitHub no longer accepts account passwords for Git commands like git push or git pull.

You have two good options to fix this. Either is fine—pick the one that fits how you like to work.

🔑 Option 1: Use a Personal Access Token (PAT) - Quick & Easy

This is the simplest fix. You'll create a token on GitHub once, then use it instead of your password.

Step 1: Generate a PAT on GitHub

  1. Log into github.com and click your profile photoSettings
  2. Scroll down and click Developer settings (at the bottom of the left sidebar)
  3. Click Personal access tokensTokens (classic)
  4. Click Generate new tokenGenerate new token (classic)
  5. Give it a descriptive name like "marapython-work" and set an expiration (90 days is common)
  6. Select the repo scope — this grants full control of your repositories (read/write)
  7. Click Generate token and copy the token immediately — you won't see it again!

Step 2: Use the token when pushing

Now go back to your terminal. When Git prompts you:

  • Username: alowree@hotmail.com (same as before)
  • Password: Paste the token you just copied (not your GitHub password)

Your push should now succeed.

Step 3: (Optional) Cache your credentials so you don't have to re-enter every time

Since you're on Arch Linux, run this to store your credentials for 1 hour:

bash
git config --global credential.helper 'cache --timeout=3600'

Now Git won't ask for your token again for an hour.

🔐 Option 2: Switch to SSH - Better for Long-Term Use

SSH is more stable once set up — no token expirations to worry about.

Step 1: Generate an SSH key (if you don't have one)

bash
ssh-keygen -t ed25519 -C "alowree@hotmail.com"

Press Enter to accept the default file location. Add a passphrase if you want extra security.

Step 2: Start the SSH agent and add your key

bash
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

Step 3: Add your public key to GitHub

bash
cat ~/.ssh/id_ed25519.pub

Copy the entire output (starts with ssh-ed25519). Then:

  1. Go to GitHub SettingsSSH and GPG keys
  2. Click New SSH key
  3. Paste your key, give it a name (e.g., "Arch Linux Laptop")
  4. Click Add SSH key

Step 4: Change your repository's remote URL from HTTPS to SSH

bash
git remote set-url origin git@github.com:Alowree/marapython-teek.git

Step 5: Test the connection

bash
ssh -T git@github.com

You should see a success message like "Hi Alowree! You've successfully authenticated...".

Now git push will just work — no username or password prompts!

📋 Which option should you choose?

OptionBest forProsCons
PAT (Option 1)Getting back to work right nowQuick setup, familiar workflowTokens expire, need to regenerate
SSH (Option 2)Long-term stabilityNever expires, more secure, no promptsSlightly more setup initially

If you just want to push your changes and move on, go with Option 1. If you'll be working with this repo regularly, Option 2 will save you headaches down the road.

Quick fix if you're in a hurry

Generate a PAT as described in Option 1, then run this one-liner to store it permanently (on Linux):

bash
git config --global credential.helper store
git push

When prompted, enter your username and paste the token. Git will save it in plain text (~/.git-credentials), so use this only on your personal, trusted machine.

I want to proceed with Option 1 as I already have the suggested Step 1~3 in place. Step 4 is the most critical step as I already had Step 1-3 in place and Step 4 was missing from my setup. Or this is related to the git clone command that I used earlier? Maybe I should have been run with the prefix git@ rather than https://? And if I have cloned with git clone git@github.com/Alowree/marapython-teek ~/OneDrive/marapython instead, then can the Step 4 suggested here be omitted? Anyways, after I took Step 4 and Step 5, the connection was success. But i still want a clear understanding what went wrong for future reference.

You've hit on the exact root cause! You're absolutely right to want a clear understanding. Let me explain what happened:

🎯 The Core Issue: HTTPS vs SSH Authentication

Yes, you're correct — if you had cloned with git@... (SSH) from the start, you would have completely avoided this problem. Here's why:

What went wrong

  1. You cloned with HTTPS: git clone https://github.com/Alowree/marapython-teek.git

    • This creates a remote called origin that points to: https://github.com/...
    • Git expects to authenticate via HTTPS (username + password/token)
  2. You tried to push with password authentication:

    • GitHub stopped accepting passwords for Git operations in August 2021
    • Your password worked fine for website login, but Git operations require a token or SSH key
  3. Step 4 fixed it because: git remote set-url origin git@github.com:...

    • This changed your existing origin remote from HTTPS to SSH
    • Now Git uses your SSH key instead of username/password

The Two Cloning Methods Compared

Clone CommandRemote URL becomesAuthentication methodWill it work with password?
git clone https://github.com/user/repo.githttps://github.com/...Username + password/token❌ No (passwords rejected)
git clone git@github.com:user/repo.gitgit@github.com:user/repo.gitSSH key✅ Yes (no password needed)

🔄 If You Had Cloned with SSH Initially

bash
git clone git@github.com:Alowree/marapython-teek.git ~/OneDrive/marapython

Then:

  • Your origin remote would already be git@github.com:Alowree/marapython-teek.git
  • Git would automatically use your SSH key (after you added it to GitHub)
  • git push would just work — no token, no password prompt
  • Step 4 would have been completely unnecessary

📝 For Future Reference

Check what protocol your remote is using

bash
git remote -v

This shows you:

  • origin https://github.com/... → HTTPS (needs token or switch to SSH)
  • origin git@github.com:... → SSH (good to go)

To switch an existing repo from HTTPS to SSH (what you did)

bash
git remote set-url origin git@github.com:username/repo.git

Or switch from SSH to HTTPS (if you wanted token-based auth)

bash
git remote set-url origin https://github.com/username/repo.git

🎓 The Key Takeaway

  • SSH keys = Set up once, works forever, no passwords/tokens
  • HTTPS + tokens = Works, but tokens expire and need regeneration
  • HTTPS + password = ❌ Doesn't work at all for Git operations anymore

Since you already have Step 5 working (SSH connection successful), you're all set! Your git push will now work seamlessly without any username/password prompts moving forward.

The fact that you identified the git clone protocol as the root issue shows you have good troubleshooting instincts — that's exactly the right way to think about this problem.

最近更新