向自有GitHub仓库推送代码时遭遇Permission Denied问题求助
Hey there, let's break down what's causing that frustrating 403 error and get your code pushed to your repo. The core issue here is that GitHub stopped accepting regular account passwords for HTTPS git operations back in 2021, plus there are a few quick checks we can do to make sure everything's set up right.
First: Double-Check Your Remote Repo URL
Sometimes the simplest things trip us up. Let's confirm your local repo is linked to the correct GitHub repo:
Run this command in your terminal:
git remote -v
You should see output like this:
origin https://github.com/xxxxx/yyyyy.git (fetch)
origin https://github.com/xxxxx/yyyyy.git (push)
If the URL doesn't match your repo's address, fix it with:
git remote set-url origin https://github.com/xxxxx/yyyyy.git
The Big Fix: Use a Personal Access Token (PAT) Instead of Your Password
GitHub no longer lets you use your regular account password to push via HTTPS. You need a Personal Access Token (PAT) to authenticate instead:
- Log into your GitHub account, go to Settings > Developer settings > Personal access tokens > Tokens (classic)
- Click "Generate new token", give it a name (like "repo-push-token"), check the repo permission box (this is all you need for pushing code), set an expiration date that works for you, then click "Generate token"
- Copy that token immediately (you won't see it again!)
- Now run
git push origin masteragain: when prompted for your username, use your GitHub handlexxxxx; for the password, paste the PAT you just generated instead of your regular login password.
Even Better: Switch to SSH (No More Password/Token Prompts!)
If you want to avoid entering credentials every time, switching to SSH is the way to go. Here's how:
- Generate an SSH key pair on your laptop:
ssh-keygen -t ed25519 -C "your-github-email@example.com"
Just hit enter through all the prompts to use the default settings.
2. Find the public key file (it's at ~/.ssh/id_ed25519.pub on macOS/Linux, or C:\Users\YourName\.ssh\id_ed25519.pub on Windows) and copy all its content.
3. Back on GitHub, go to Settings > SSH and GPG keys > New SSH key, paste the public key content, give it a name, and save.
4. Update your local repo's remote URL to use SSH:
git remote set-url origin git@github.com:xxxxx/yyyyy.git
Now when you run git push origin master, it'll authenticate via SSH automatically—no more typing credentials!
Quick Sanity Check: Confirm You Own the Repo
Since this is your own repo, this is probably fine, but just to be sure: head to your GitHub repo page (https://github.com/xxxxx/yyyyy) and check that you're listed as the Owner in the repo details (top right corner). If you were a collaborator on someone else's repo, you'd need push permissions, but since it's yours, this shouldn't be the issue.
Follow these steps, and you'll have your code pushed up in no time!
内容的提问来源于stack exchange,提问作者user1443098

