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

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

问题描述

我有一个工作的GitHub帐户和一个私人帐户。首先,我使用个人测试项目,然后我继续并在同一台计算机上与另一个帐户做了一个存储库。



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

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

但现在告诉我


错误:个人用户/ newrepo.git被拒绝的权限


我没有了解另一个帐户如何连接到这个帐户。

>解决方案

这听起来与我目前的工作非常相似。看起来你已经设置了单独的 ssh-keys ,所以你还需要创建一个〜/ .ssh / config

 主机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 一个。我不会试图解释这是什么,但这是在我目前的设置和工作正常。



值得注意的是,主机URL 只是一个指针,用于获取正确的用户设置,并且不会影响正确获取文件到目标 HostName url。 / p>

现在您只需确保您的来源(或任何远程)的url会根据您的用户名与您各自的仓库中正确的 Host url匹配。如果您已经有个人回购,您可以在文本编辑器中编辑该回购的 .git / config 文件:

  

或通过命令行执行:

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

与您的工作类似:

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

或者再次通过命令行:

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

当然,你可以随时在〜/ .ssh / config 主机 url中的一个c $ c> file作为

 主机github.com 

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



完成这些设置后,您应该可以推送到每个相应的远程设备。

编辑



有一件事需要注意,我刚刚发现自己是,如果您为您的 user.email 值设置全局git配置值(并且我在猜测 user.name 也会发送一个不同的值),git会显示你提交的那个邮件用户。为了解决这个问题,你可以覆盖你本地仓库中的全局git配置设置:



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

现在应该发送up作为该回购的正确用户提交。


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.

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

but that now tells me

ERROR: Permission to personaluser/newrepo.git denied to

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

解决方案

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

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.

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.

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

or do it via command line:

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

Likewise to your work one:

[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

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

Host 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.

EDIT

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天全站免登陆