如何使用其他用户名推送到GitHub?
我和一个朋友正在共享我的电脑。我已经在Windows7上使用GitBashShell推送到GitHub。现在我们在那台电脑上做一个不同的项目,我需要她转到她的账户。但它一直试图使用我的用户名,并说我无权访问她的存储库:如何使用其他用户名推送到GitHub?,git,github,Git,Github,我和一个朋友正在共享我的电脑。我已经在Windows7上使用GitBashShell推送到GitHub。现在我们在那台电脑上做一个不同的项目,我需要她转到她的账户。但它一直试图使用我的用户名,并说我无权访问她的存储库: $ git push her_github_repository our_branch ERROR: Permission to her_username/repository.git denied to my_username. fatal: The remote end hu
$ git push her_github_repository our_branch
ERROR: Permission to her_username/repository.git denied to my_username.
fatal: The remote end hung up unexpectedly
如果您使用不同的windows用户,您的SSH密钥和git设置将是独立的 如果这不是您的选项,那么您的朋友应该将您的SSH密钥添加到她的Github帐户 虽然,之前的解决方案会让你们像自己一样努力,但它会让你们推进她的回购协议。如果您不想这样做,并且在同一台电脑上的不同文件夹中工作,您可以通过删除config命令的
-g
标志,在带有git的文件夹中本地设置用户名和电子邮件:
git config user.name her_username
git config user.email her_email
或者,如果您使用https协议,Github每次都会提示输入用户名/密码(除非您使用密码管理器)。如果在运行
git push
git请求输入user
的密码,但您希望以新用户身份推送,您可能需要使用git config remote.origin.url
:
$ git push
user@my.host.com:either/branch/or/path's password:
此时,使用^C
退出git push
,并使用以下命令作为新用户推送
$ git config remote.origin.url
user@my.host.com:either/branch/or/path
$ git config remote.origin.url new_user@my.host.com:either/branch/or/path
$ git push
new_user@my.host.com:either/branch/or/path's password:
我使用自定义的IdentityFile
设置ssh别名,并重写源代码以使用自定义的me github
主机名
#when prompted enter `id_rsa_user1` as filename
ssh-keygen -t rsa
# ~/.ssh/config
Host user1-github
HostName github.com
Port 22
User git
IdentityFile ~/.ssh/id_rsa_user1
#check original remote origin url
git remote -v
origin git@github.com:user1/my-repo.git
#change it to use your custom `user1-github` hostname
git remote rm origin
git remote add origin git@user1-github:user1/my-repo.git
如果使用SSH URL(git@github.com:username/projectname.git
)而不是HTTPS URL,您可以使用$git\u SSH\u命令
环境变量临时指定不同的SSH密钥:
$ GIT_SSH_COMMAND="ssh -i different_private_key" git push
据我所知,对于SSH URL,GitHub不关心用户名,只关心密钥:如果用户帐户可以访问存储库,并且该帐户具有SSH密钥(请参阅帐户设置中的),那么如果您使用该SSH密钥推送到该存储库,推送将被视为来自该用户。如前所述,你可以用
git config user.name her_username
git config user.email her_email
要手动设置单个回购的用户名和电子邮件,但必须添加以下命令:
git commit --amend --reset-author
如果您在更改之前已尝试推送。否则,更改不会显示在配置文件中。转到凭据管理器
转到Windows凭据
删除通用凭据下的条目
再次尝试连接。如果您有
然后您可以转到->帐户
然后注销并登录。git config user.name仅更改我提交的名称。我还是推不动。这就是我解决问题的方法,我认为这对我来说是一个简单的方法
在要在将要使用的计算机上推送的用户名下生成SSH密钥
将此密钥添加到要推送到的github用户帐户
选择使用SSH进行克隆
您现在可以将此用户推送到该repo。克隆时很简单,请将git URL与您的用户名一起使用。提交时将询问您的新用户密码
例如:
git克隆
git clone如果这是您的问题
remote: Permission to username1/repo.git denied to username2.
fatal: unable to access 'https://github.com/username1/repo.git/':
The requested URL returned error: 403
除了使用git config从终端更改用户名和电子邮件:
$ git config --global user.name "Bob"
$ git config --global user.email "bob@example.com"
您需要从密钥链中删除授权信息。这个解决方案花了我几个小时才弄明白。我发现我的钥匙链中也有证书
打开钥匙链访问,单击所有项目并搜索git。删除所有钥匙链您可以使用不同的帐户推送。
例如,如果您的帐户是存储在.gitconfig中的A,并且您想要使用帐户B,它是您想要推送的回购的所有者
帐户B:B_用户名、B_密码
SSH链接的示例:
B帐户的推送是:
$ git push https://'B_user_name':'B_password'@github.com/B_user_name/project.git
在.gitconfig中查看帐户
$git config--global--list
$git config--global-e
(同时更改帐户)
如果在Windows和用户Git for Windows下,使用OAuth
令牌通过https
推送到GitHub时,无法在用户之间轻松切换
原因是令牌存储为:
- 互联网地址:
git:https://github.com
- 用户名:
个人访问令牌
- 密码:
OAuth\u令牌
互联网地址中URL的变体不起作用,例如:
git:https://username@github.com
git:https://github.com/username
解决方案:名称空间。这可以在Windows的Git Credential Manager配置的详细信息中找到:
引述:
名称空间
为存储的凭据设置命名空间
默认情况下,GCM对所有存储的凭据使用“git”命名空间,设置此配置值允许控制全局或每个主机使用的命名空间
git config --global credential.namespace name
现在,将您的凭据存储在Windows凭据管理器中,如下所示:
- 互联网地址:
git.username:https://github.com
- 用户名:
个人访问令牌
- 密码:
OAuth\u令牌
请注意,我们已经更改:git
->git.username
(您可以将username
更改为您的实际用户名,或者为了它,您可以将其更改为您想要的唯一标识符)
现在,在要使用特定条目的存储库中,执行:
git config credential.namespace git.username
(再次…用所需值替换username
)
您的.git/config
现在将包含:
[credential]
namespace = git.username
瞧!将从Windows凭据存储中提取正确的凭据
当然,这不会改变正在推送的用户/电子邮件。为此你
git config --local credential.helper ""
git push origin master
#first account
Host github.com-first
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
#another account
Host github.com-another
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_another
ssh-add -D
ssh-add ~/.ssh/ssh_key_for_my_2nd_account
git push
ssh-add -D
ssh-add ~/.ssh/id_rsa
git push
# you can check what's currently:
git config user.name
git config user.email
git config user.name "your_github_username"
git config user.email "your_github_email"
# Again check what's currently:
git config user.name
git config user.email
git log
# once you're confirmed that it's tagged to you, then you should move to step 3
git config --local credential.helper ""
git push
# it will ask you to enter your github_username and github_password
git add .
git commit -m "initial commit"
git config --local credential.helper ""
git push https://github.com/youraccount/repo.git --all