首页 > 解决方案 > git push 错误:权限拒绝

问题描述

一个月前,我在我的mac上配置了两个github账号:一个是工作账号,一个是个人账号。我已经用了一个月没有问题。在一天中提交/推送/等以回购工作。晚上,在几个个人回购中提交/推送/等。突然,今天我无法在我的一个个人帐户中推送一些更改,错误是:

git push --set-upstream origin <branch>
ERROR: Permission to <repo>.git denied to <work-account>.
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我不知道为什么 git push 命令试图使用我的工作帐户进行推送!?!?!?

我之前的更改和推送是在 7 天前使用我的个人帐户(没有问题)。我没有更改配置中的任何内容(ssh、.git/config 文件等)!

我在 repo 的目录中尝试了这个:

git config user.email
git config user.name

电子邮件是我的个人电子邮件,所以这是正确的。

为什么 git 使用我的工作帐户?即使git config命令显示我的个人帐户?此外,这是几天前工作的......

笔记2:

Host github.com
  Hostname github.com
  User git
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_rsa

Host <githubpersonal>
  Hostname github.com
  User git
  IdentityFile ~/.ssh/id_rsa_<personal_account>
[remote "origin"]
    url = git@<githubpersonal>:<personal_account>/<repo>.git
    fetch = +refs/heads/*:refs/remotes/origin/*
origin  git@<githubpersonal>:<personal_account>/<repo>.git (fetch)
origin  git@<githubpersonal>:<personal_account>/<repo>.git (push)

我不知道为什么不再工作了......

标签: gitgithubssh

解决方案


检查您的配置文件是否有任何 pushURL 配置,以检查它是否尝试使用您不期望的 URL 进行推送。

注意:您可以测试的 SSH URL 是githubpersonal,不是git@githubpersonal,因为 git 已经在~/.ssh/config

ssh -Tv githubpersonal

推荐阅读