You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Git错误用户认证问题求助:多种尝试方案均无效

Git Persists Using Wrong User for Authentication Despite Disabling Credential Helpers

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.helper at 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 -v error 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 of refs/heads/master from 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)

  1. 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.
  2. Verify no hidden credential helpers are active:
    Run this command to see all active credential helper configurations across all levels:
    git config --show-origin --get credential.helper
    
    If any output shows a helper you didn't unset (like libsecret or gnome-keyring), you can remove it with the corresponding --unset command (e.g., sudo git config --system --unset credential.helper if it's in /etc/gitconfig).
  3. 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:
    git remote set-url github git@github.com:{redacted}.git
    
    Since you already use SSH, this should work without extra setup, and you won't have to deal with HTTPS auth anymore.

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

火山引擎 最新活动