Git错误用户认证问题求助:多种尝试方案均无效
Let's unpack your problem step by step—you've tried all the usual credential-reset steps, but Git keeps defaulting to the wrong user for HTTPS auth, and only hardcoding the desired user in the remote URL fixes it. Here's why that's happening, and what you might have missed:
Why Your Initial Troubleshooting Didn't Resolve the Issue
You covered most common credential sources, but Git 2.11.0 (even on Ubuntu) can pull credentials from system-level keyring integrations that don't show up in the usual git config or .git-credentials files. Specifically:
- Ubuntu's Gnome Keyring/Libsecret: Git often hooks into the system's default password manager on Linux. Even if you unset
credential.helperat all levels, the keyring might still be serving up cached credentials for the wrong GitHub user. You didn't mention checking this, which is likely the root cause. - The
git pull -verror about missing refs is a side effect: because authentication failed with the wrong user, Git couldn't fetch any remote branches, so your local repo didn't have a record ofrefs/heads/masterfrom the remote. It wasn't a branch existence issue—it was an auth failure blocking the fetch entirely.
Why Your Workaround Worked
When you added {desired-user@} to the remote URL (https://{desired-user@}github.com/{redacted}), you're forcing Git to explicitly use that username for every HTTPS request to that repo. This bypasses all external credential sources (keyring, cached helpers, etc.) because Git prioritizes the username embedded directly in the URL.
The [credentials] block you added is actually redundant here—Git would have used the username from the URL even without it. The plaintext password in that block works, but it's not secure (anyone with access to your repo can see it), so I'd recommend removing that if you can.
Steps to Fix This Permanently (Without Hardcoding Credentials)
- Clear the system keyring:
- Open Ubuntu's Passwords and Keys app (search for it in the launcher).
- Look for entries labeled with GitHub's URL (e.g.,
https://github.com). Delete any that are associated with the wrong user.
- Verify no hidden credential helpers are active:
Run this command to see all active credential helper configurations across all levels:
If any output shows a helper you didn't unset (likegit config --show-origin --get credential.helperlibsecretorgnome-keyring), you can remove it with the corresponding--unsetcommand (e.g.,sudo git config --system --unset credential.helperif it's in/etc/gitconfig). - Switch back to SSH (recommended, since you already use it):
Update your remote URL to use SSH instead of HTTPS—this avoids HTTPS credential issues entirely:
Since you already use SSH, this should work without extra setup, and you won't have to deal with HTTPS auth anymore.git remote set-url github git@github.com:{redacted}.git
Your Temporary Workaround (For Reference)
Here's the config change that worked for you (with sensitive info redacted):
Removed:
[remote "github"] url = https://github.com/{redacted} fetch = +refs/heads/*:refs/remotes/github/*
Added:
[credentials "https://{desired-user@}github.com/{redacted}"] username = {desired-user} password = {redacted} [remote "github"] url = https://{desired-user@}github.com/{redacted} fetch = +refs/heads/*:refs/remotes/github/*
内容的提问来源于stack exchange,提问作者wvxvw




