Git 以错误的用户身份推送到远程 GitHub 存储库 [英] Git pushing to remote GitHub repository as wrong user

查看:36
本文介绍了Git 以错误的用户身份推送到远程 GitHub 存储库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个工作 GitHub 帐户和一个个人帐户.首先,我使用个人帐户进行测试项目,然后我继续使用同一台计算机上的另一个帐户创建了一个存储库.

I have a work GitHub account and a personal one. First I used the personal one for test projects, then I moved on and did a repository with the other account on the same computer.

现在我想再次在我的个人帐户上创建一个新存储库,我更改了全局和本地user.name,并做了一个新的 ssh 密钥对,在 GitHub 设置页面中输入.然后我设置目录

Now I wanted to create a new repository on my personal account again, I changed the global and local user.name, and did a new ssh key pair, entered in the GitHub setup page. Then I set up the directory

git init
git remote add origin <url>
git push origin

但现在告诉我

错误:对personaluser/newrepo.git 的权限被拒绝

ERROR: Permission to personaluser/newrepo.git denied to

我不知道另一个帐户是如何连接到这个帐户的..git/config 没有显示与 workusername 相关的东西.

I have no idea how the other account is connected to this one. .git/config shows no workusername related things.

如果您使用的是 Windows 10,请花点时间阅读 Rajan 的回答.

推荐答案

这听起来与我目前的工作安排非常相似.您似乎已经设置了单独的 ssh-keys,因此您还需要创建一个 ~/.ssh/config 文件并使用类似于以下内容的信息填充它:

this sounds very similar to my current work set up. it seems that you already have set up your separate ssh-keys so you also need to create a ~/.ssh/config file and populate it with information similar to this:

Host work.github.com
    HostName github.com
    User WORK_GITHUB_USERNAME
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_work_rsa
    IdentitiesOnly yes

Host personal.github.com
    HostName github.com
    User PERSONAL_GITHUB_USERNAME 
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_personal_rsa
    IdentitiesOnly yes

每个属性听起来都很自我解释,但 IdentitiesOnly 一个.我不会试图解释它的用途,但这是在我当前的设置中并且工作正常.

Every property sounds pretty self explanatory but the IdentitiesOnly one. I won't try to explain what that is for, but that is in my current setup and works fine.

还值得注意的是,Host URL 只是一个获取正确用户设置的指针,对将文件正确发送到目标 HostName 没有任何影响网址.

It's also worth noting that the Host URL is just a pointer to grab the correct user settings and does not have any affect on getting the files correctly to your target HostName url.

现在您只需要确保您的 origin(或任何一般的 remote)url 与您各自存储库中的正确 Host url 匹配取决于您的用户名.如果您已经有现有的个人存储库,您可以在文本编辑器中编辑该存储库的 .git/config 文件:

Now you just need to make sure your origin (or any remote in general) url match the correct Host url in your respective repos depending on your user name. If you already have existing personal repos, you can edit that repo's .git/config file in your text editor:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@personal.github.com:PERSONAL_GITHUB_USERNAME/project.git

或通过命令行执行:

git remote set-url origin git@personal.github.com:PERSONAL_GITHUB_USERNAME/project.git

与您的工作一样:

[remote "origin"]
    fetch = +refs/heads/*:refs/remotes/origin/*
    url = git@work.github.com:your_work_organization/project.git

或者再次,通过命令行:

or again, via command line:

git remote set-url origin git@work.github.com:your_work_organization/project.git

当然,你总是可以在你的 ~/.ssh/config 文件中设置你的 Host 网址之一

Of course, you can always set one of your Host urls in your ~/.ssh/config file as just

Host github.com

我只使用了 work.github.com 来更容易地查看配置关系.

I only used work.github.com to see the config relationships easier.

一旦这些都设置好,您应该能够推送到每个相应的遥控器.

Once these are all set, you should be able to push to each respective remote.

编辑

我刚刚发现的一件事要注意的是,如果您曾经为 user.email 值设置了全局 git 配置值(我猜是 user.name 也会发送不同的值),git 将显示您作为该电子邮件用户的提交.要解决此问题,您可以覆盖本地存储库中的全局 git 配置设置:

One thing to note that I just found out myself is that if you ever set global git config values for your user.email value (and i'm guessing user.name would send a different value as well), git will show your commits as that email user. To get around this, you can override the global git config settings within your local repository:

$ git config user.name "John Doe"
$ git config user.email johndoe@example.com

现在应该将提交作为该存储库的正确用户发送.

This should now send up commits as the correct user for that repo.

这篇关于Git 以错误的用户身份推送到远程 GitHub 存储库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆