git 如何以不同的用户名推送到 GitHub?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13103083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I push to GitHub under a different username?
提问by at.
A friend and myself are sharing my computer. I've made pushes to GitHub using the git bash shell on Windows 7. Now we're in a different project on that computer and I need her to push to her account. But it keeps trying to use my username and saying I don't have access to her repository:
一个朋友和我正在共用我的电脑。我已经在 Windows 7 上使用 git bash shell 推送到 GitHub。现在我们在该计算机上的另一个项目中,我需要她推送到她的帐户。但它一直试图使用我的用户名并说我无权访问她的存储库:
$ 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
采纳答案by Simon Boudrias
If you use different windows user, your SSH key and git settings will be independent.
如果您使用不同的 Windows 用户,您的 SSH 密钥和 git 设置将是独立的。
If this is not an option for you, then your friend should add your SSH key to her Github account.
如果这不是您的选择,那么您的朋友应该将您的 SSH 密钥添加到她的 Github 帐户。
Although, previous solution will keep you pushing as yourself, but it will allow you to push into her repo. If you don't want this and work in different folder on the same pc, you can setup username and email locally inside a folder with git by removing -g
flag of the config command:
虽然,以前的解决方案会让你像你自己一样推动,但它会让你进入她的回购。如果你不想要这个并且在同一台电脑上的不同文件夹中工作,你可以通过删除-g
config 命令的标志在带有 git 的文件夹中本地设置用户名和电子邮件:
git config user.name her_username
git config user.email her_email
Alternatively, if you push over https
protocol, Github will prompt for username/password every time (unless you use a password manager).
或者,如果你推送https
协议,Github 每次都会提示输入用户名/密码(除非你使用密码管理器)。
回答by s-hunter
This worked for me, it will prompt for username and password
这对我有用,它会提示输入用户名和密码
git config --local credential.helper ""
git push origin master
回答by thotruong
You can push with using different account. For example, if your account is Awhich is stored in .gitconfig and you want to use account Bwhich is the owner of the repo you want to push.
您可以使用不同的帐户进行推送。例如,如果您的帐户是存储在 .gitconfig 中的A,并且您想使用帐户B,它是您要推送的存储库的所有者。
Account B: B_user_name, B_password
Example of SSH link: https://github.com/B_user_name/project.git
账号B: B_user_name, B_password
SSH 链接示例:https: //github.com/B_user_name/project.git
The push with Baccount is:
B账户的推送是:
$ git push https://'B_user_name':'B_password'@github.com/B_user_name/project.git
To see the account in .gitconfig
查看 .gitconfig 中的帐户
$git config --global --list
$git config --global -e
(to change account also)
$git config --global --list
$git config --global -e
(也可以换账号)
回答by chovy
I setup an ssh alias using a custom IdentityFile
and rewrote the origin to use my custom me-github
hostname.
我使用自定义设置了 ssh 别名IdentityFile
并重写了源以使用我的自定义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 [email protected]: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
回答by David Moles
If you use an SSH URL ([email protected]:username/projectname.git
) rather than an HTTPS URL, you can temporarily specify a different SSH key using the $GIT_SSH_COMMAND
environment variable:
如果您使用 SSH URL ( [email protected]:username/projectname.git
) 而不是 HTTPS URL,您可以使用$GIT_SSH_COMMAND
环境变量临时指定不同的 SSH 密钥:
$ GIT_SSH_COMMAND="ssh -i different_private_key" git push
As far as I can tell, with SSH URLs, GitHub doesn't care about user names, only about keys: if a user account has access to a repository, and that account has an SSH key (see the SSH keys pagein the account settings), then if you use that SSH key to push to that repository, the push will be treated as coming from that user.
据我所知,使用 SSH URL,GitHub 不关心用户名,只关心密钥:如果用户帐户可以访问存储库,并且该帐户具有 SSH 密钥(请参阅帐户中的SSH 密钥页面设置),那么如果您使用该 SSH 密钥推送到该存储库,则推送将被视为来自该用户。
回答by Lukasz Czerwinski
If after running git push
Git asks for a password of user
, but you would like to push as new_user
, you may want to use git config remote.origin.url
:
如果在运行git push
Git 后要求输入密码user
,但您想推送为new_user
,您可能需要使用git config remote.origin.url
:
$ git push
[email protected]:either/branch/or/path's password:
At this point use ^C
to quit git push
and use following to push as new_user
.
此时使用^C
退出git push
并使用以下推送 as new_user
。
$ git config remote.origin.url
[email protected]:either/branch/or/path
$ git config remote.origin.url [email protected]:either/branch/or/path
$ git push
[email protected]:either/branch/or/path's password:
回答by Krishnamoorthy Acharya
It's simple while cloning please take the git URL with your username.While committing it will ask your new user password.
克隆时很简单,请使用您的用户名获取 git URL。提交时会询问您的新用户密码。
Eg:
例如:
git clone https://[email protected]/username/reponame.git
git clone https://[email protected]/username/reponame.git
回答by Tomasz Bartkowiak
I have been using one machine to push code to two different GitHub accounts with different username. Assuming you already set up one account and want to add a new one:
我一直在使用一台机器将代码推送到两个不同用户名的不同 GitHub 帐户。假设您已经设置了一个帐户并想添加一个新帐户:
- Generate new SSH key
ssh-keygen -t rsa -C "[email protected]"
- Save it, but remember not to override the existent one
id_rsa
. Give it a different name, e.g.id_rsa_another
- Copy the contents of the key to your GitHub account:
- 生成新的 SSH 密钥
ssh-keygen -t rsa -C "[email protected]"
- 保存它,但记住不要覆盖现有的
id_rsa
。给它一个不同的名字,例如id_rsa_another
- 将密钥的内容复制到您的 GitHub 帐户:
Settings -> SSH and GPG keys -> New SSH key -> Give a label and paste the key-> Add SSH key
设置 -> SSH 和 GPG 密钥 -> 新建 SSH 密钥 ->给标签并粘贴密钥-> 添加 SSH 密钥
- Add the key to the ssh agent:
ssh-add ~/.ssh/id_rsa_another
- Setup a GitHub host: Create a config file with
touch ~/.ssh/config
and edit the file by providing configurations to your accounts:
- 将密钥添加到 ssh 代理:
ssh-add ~/.ssh/id_rsa_another
- 设置 GitHub 主机:创建一个配置文件
touch ~/.ssh/config
并通过为您的帐户提供配置来编辑该文件:
#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
Now you should be able to push from different accounts depending on what key you add to the ssh agent, i.e. to use your first account, do
ssh-add ~/.ssh/id_rsa
.
现在您应该能够根据您添加到 ssh 代理的密钥从不同的帐户推送,即使用您的第一个帐户,请执行
ssh-add ~/.ssh/id_rsa
.
You might also want to change your user email:
您可能还想更改您的用户电子邮件:
git config --global user.email "[email protected]"
git config --global user.email "[email protected]"
or clean out ssh keys in case of permission error when pusing code to one of the accounts:
或在将代码推送到其中一个帐户时出现权限错误时清除 ssh 密钥:
ssh-add -D
ssh-add -D
回答by Muyide Ibukun
if this is your problem
如果这是你的问题
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
In addition to changing username and email from terminal using git config:
除了使用 git config 从终端更改用户名和电子邮件之外:
$ git config --global user.name "Bob"
$ git config --global user.email "[email protected]"
you'll need to remove authorization info from Keychain. This solution took me several hours to figure out.I found that I also had certificate in my Keychain.
您需要从钥匙串中删除授权信息。这个解决方案花了我几个小时才弄清楚。我发现我的钥匙串中也有证书。
Open up Keychain access, click on All Items and search for git. Delete all keychain
打开钥匙串访问,单击所有项目并搜索 git。删除所有钥匙串
回答by Deepak
Go to Credential Manager Go to Windows Credentials Delete the entries under Generic Credentials Try connecting again.
转到凭据管理器 转到 Windows 凭据 删除通用凭据下的条目 再次尝试连接。