Windows系统下使用Git命令行上传项目至GitHub失败求助
Hey there, let's work through fixing this Git push issue step by step—uploading projects to GitHub from the Windows command line can throw unexpected hurdles, even for folks who’ve done it before. Let’s break down the most common fixes:
1. Verify Your Git User Configuration
GitHub needs to know who you are to accept your pushes. Open Git Bash or Command Prompt and run these commands (replace with your own GitHub username and email):
git config --global user.name "YourGitHubUsername" git config --global user.email "your-github-email@example.com"
If you skip this, GitHub will reject your commits outright.
2. Pick the Right Authentication Method (SSH vs HTTPS)
Option 1: HTTPS (Quick Setup)
If you’re using HTTPS, your remote repo URL looks like https://github.com/YourUsername/YourRepo.git. Common issues here:
- 2-Factor Authentication (2FA) enabled: You can’t use your regular GitHub password anymore. Instead, generate a Personal Access Token (PAT) to use as your password:
- Go to GitHub → Settings → Developer settings → Personal access tokens → Tokens (classic)
- Generate a token with the
repopermission checked, save it somewhere safe (you won’t see it again!) - When prompted for a password during
git push, paste this token instead.
- Cached wrong credentials: Open Windows Credential Manager (search for it in the Start menu), go to "Windows Credentials", find any entries for
github.com, delete them, then try pushing again—you’ll be prompted to enter your credentials (or PAT) fresh.
Option 2: SSH (No More Password Prompts)
For long-term use, SSH is more convenient. Here’s how to set it up:
- Generate an SSH key in Git Bash:
ssh-keygen -t ed25519 -C "your-github-email@example.com"
Press Enter through all prompts to accept default settings—this creates id_ed25519 (private key) and id_ed25519.pub (public key) in C:\Users\YourUsername\.ssh.
2. Copy the public key: Open id_ed25519.pub with Notepad, select all, and copy it.
3. Add it to GitHub: Go to GitHub → Settings → SSH and GPG keys → New SSH key. Paste the key, give it a descriptive name, and save.
4. Test the connection:
ssh -T git@github.com
If you see Hi YourUsername! You've successfully authenticated, but GitHub does not provide shell access., you’re good to go.
A wrong remote URL is a super common culprit. Run this to list your current remotes:
git remote -v
- If nothing shows up, add the correct remote:
# HTTPS git remote add origin https://github.com/YourUsername/YourRepo.git # OR SSH git remote add origin git@github.com:YourUsername/YourRepo.git - If the URL is wrong, delete the old remote first:
git remote remove origin
Then re-add the correct one as above.
Make sure you’re in your project’s root folder, then run these steps in order:
- Initialize the repo (if you haven’t already):
git init
- Stage all files for commit:
git add .
- Commit your changes with a descriptive message:
git commit -m "Initial commit: Add project files"
- Push to GitHub (the
-usetsorigin mainas your default upstream branch):
git push -u origin main
Note: GitHub uses main as the default branch now (not master), so double-check you’re using the right branch name.
fatal: remote origin already exists: You already added a remote—just delete it withgit remote remove originthen re-add the correct URL.Permission denied (publickey): Double-check that your SSH public key is correctly added to GitHub, or switch to HTTPS with a PAT.fatal: unable to access 'https://github.com/...': OpenSSL SSL_read: Connection was reset, errno 10054: This is usually a network issue. Try switching to a different network, or if you use a proxy, configure Git to use it:
To remove the proxy later:git config --global http.proxy http://your-proxy-address:port git config --global https.proxy http://your-proxy-address:portgit config --global --unset http.proxy git config --global --unset https.proxyerror: failed to push some refs to 'github.com:...': This means the remote repo has content you don’t have locally (like a README you created on GitHub’s web interface). Pull and merge first:
Then rungit pull origin main --allow-unrelated-historiesgit pushagain.
内容的提问来源于stack exchange,提问作者Rehman Maqsood

