ZhangZhihui's Blog  

 

$ git push -u origin main 
remote: Invalid username or token. Password authentication is not supported for Git operations.

 

GitHub no longer supports username/password authentication for Git operations. Since August 2021, you must use one of these methods instead:


1. Use a Personal Access Token (PAT)

  1. Go to GitHub Personal Access Tokens.

  2. Click “Generate new token” → give it a name and expiration (e.g., 90 days).

  3. Select scopes:

    • repo → for full repository access (read/write).

    • workflow → if you want to push workflow files.

  4. Click Generate token, copy the token once (you won’t see it again).

Now, in Git Bash:

git remote set-url origin https://<YOUR_USERNAME>@github.com/<USERNAME>/<REPO>.git

When it prompts for a password, use the token instead of your GitHub password.


2. Use SSH Authentication (recommended)

  1. Check if you have an SSH key:

ls ~/.ssh/id_rsa.pub
  1. If not, generate one:

ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  1. Copy the public key:

cat ~/.ssh/id_rsa.pub
  1. Add it to GitHub: Settings → SSH and GPG keys → New SSH key

  2. Change the Git remote to SSH:

git remote set-url origin git@github.com:<USERNAME>/<REPO>.git

Now you can push without entering username/password:

git push -u origin main

⚡ Quick Notes

  • Using PAT over HTTPS is simpler if you don’t want to configure SSH.

  • Once you enter a PAT once, Git can cache it using the Git Credential Manager on Windows.

 

posted on 2025-09-02 20:50  ZhangZhihuiAAA  阅读(197)  评论(0)    收藏  举报