本地 git 配置未覆盖项目的全局用户

Local git config not overriding global user for project

我配置了全局 git 用户,但想为单个 git 项目使用不同的用户。

在该项目中,我使用 git config --local user.name "localuser"git config --local user.email "localuser@example.com" 设置本地项目的用户和电子邮件。

但是,当我尝试在 github 上推送到我的遥控器时,出现此错误:

remote: Permission to localuser/repo.git denied to globaluser.
fatal: unable to access 'https://github.com/localuser/repo.git/': The requested URL returned error: 403

下面是一些可能有助于诊断的输出:

git remote -v:

github  https://github.com/localuser/repo.git (fetch)
github  https://github.com/localuser/repo.git (push)

git config --list:

user.name=globaluser
user.email=globaluser@example.com
...

git config --local --list:

user.name=localuser
user.email=localuser@example.com
...

git config user.name:

localuser

我已经提交了我的更改并收到了我的全局用户拒绝的权限。随后设置本地用户没有任何作用,但 git config user.name 报告了正确的本地用户。

有效的是 (courtesy of this google groups thread):

git commit --amend --reset-author

我假设提交的更改附有原作者。

如果您使用 GitHub 处理 OSX,则可能是证书问题。您的 GitHub 证书会记住您的 user.name 和 user.email 覆盖本地配置设置。解决它的一种方法是转到您的钥匙串并删除 GitHub 证书。

尝试了很多方法,花了很多时间,但没有任何效果。最后我不得不清除所有用户:

git config --local --unset credential.helper
git config --global --unset credential.helper
git config --system --unset credential.helper

我现在被要求再次推送我的 GitHub 凭据,我可以提供正确的用户 ID 和密码:)

如果使用 windows 也可以通过 Control Panel\User Accounts\Credential Manager

删除

就我而言,重置提交作者不起作用:

问题是组合:

  • 我的默认 ssh 密钥 id_rsa.pub 是由我的全局用户
  • 在 github 上设置的
  • 我的本地用户没有在 github
  • 上设置 ssh-key

因此您的 git 将使用链接到全局用户

的默认值 id_rsa

解决方法是:

  • 使用 ssh-keygen 生成一个新的 ssh 密钥,比方说 id_2 & id_2.pub,将这个 ssh 密钥添加到 github
  • git config --add --local core.sshCommand 'ssh -i <<<PATH_TO_SSH_KEY>>>' 将为本地存储库设置带有新 ssh 密钥的 ssh 选项

完成。