GitBash推送至GitHub报错:Fatal: master cannot be resolved to branch
Hey there! Let's work through this frustrating error you're hitting when trying to push your master branch to GitHub. Since you've already checked off the basics—logging into your account, setting your global Git config with name/email, committing all your changes, and confirming you're on the master branch—here are the most common fixes that should get you back on track:
1. Verify Your Branch & Remote Setup First
First, let's double-check what branches exist locally and remotely to rule out any mismatches:
- Run
git branch -ato list all local and remote branches. You should seemaster(local) andremotes/origin/master(remote) if the branch exists on GitHub. - If
remotes/origin/masterdoesn't show up, that means the master branch hasn't been created on GitHub yet—we'll fix that in step 3.
2. Fix the Upstream Tracking Relationship
This error often happens when your local master branch isn't linked (tracking) to the remote origin/master branch. To set that up:
- Run this command to establish the tracking connection:
git branch --set-upstream-to=origin/master master - Once that's done, try pushing again with:
git push
3. Push for the First Time (If Remote Branch Doesn't Exist)
If this is your first time pushing the master branch to GitHub (so remotes/origin/master doesn't exist yet), use the -u flag to create the remote branch and set up tracking in one go:
git push -u origin master
The -u flag tells Git to remember this upstream relationship, so future pushes can just use git push without extra arguments.
4. Refresh Your Remote Info
Occasionally, Git might have outdated info about your remote repository. Run this to fetch the latest remote branch data:
git fetch origin
Then try the push command again.
5. Double-Check Your Branch Name
It sounds silly, but typos happen! Run git branch to confirm your current local branch is indeed named master (note: GitHub uses main as the default branch for new repos these days—if your repo was created recently, you might need to replace master with main in all the commands above).
If none of these work, feel free to share the output of git branch -a and git remote -v and we can dig deeper!
内容的提问来源于stack exchange,提问作者Tawnwen

