git 使用多个帐户从命令行登录 GitHub
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34731832/
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
Log in to GitHub from the command line with multiple accounts
提问by MrE
I opened a new GitHub account to separate my business vs. personal repositories.
我开设了一个新的 GitHub 帐户来区分我的企业和个人存储库。
Now, I git init
my local repository and git add remote origin <the repository HTTPS URL>
现在,git init
我的本地存储库和git add remote origin <the repository HTTPS URL>
I try to push, and it seems to always take the credentials of my original account, not prompting me for the credentials for the new account.
我尝试推送,它似乎总是采用我原始帐户的凭据,而不是提示我输入新帐户的凭据。
I tried using the URL format with
我尝试使用 URL 格式
https://<username>:<password>@github.com/<username>/<repository.git>
but that doesn't seem to help: I still get an error that the credentials are invalid for the original account username.
但这似乎无济于事:我仍然收到一个错误,即原始帐户用户名的凭据无效。
How do I log in with multiple sets of credentials or how would I somehow reset the original credentials to force password prompt when pushing?
我如何使用多组凭据登录,或者如何在推送时以某种方式重置原始凭据以强制密码提示?
The only way I managed to push right now is by specifying the username:[email protected]/
in the URL in the git push
command.
我现在设法推送的唯一方法是username:[email protected]/
在git push
命令的 URL 中指定。
采纳答案by MrE
The only way I managed to push was by specifying the username:[email protected]/
in the URL in the Git push command.
我设法推送的唯一方法是username:[email protected]/
在 Git push 命令的 URL 中指定。
回答by CodeWizard
git config --global credential.helper cache
... which tells git to keep your password cached in memory for (by default) 15 minutes. You can set a longer timeoutwith:
...它告诉 git 将您的密码缓存在内存中(默认情况下)15 分钟。您可以设置更长的超时时间:
git config --global credential.helper "cache --timeout=3600"
More useful links:
更多有用的链接:
Using the .netrc file The .netrc file is a mechanism that allows you to specify which credentials to use for which server.
This method allows you to avoid entering a username and password every time you push to or pull from Git, but your Git password is stored in plain text.
使用 .netrc 文件 .netrc 文件是一种机制,允许您指定用于哪个服务器的凭据。
这种方法可以让您避免每次推送到 Git 或从 Git 拉取时都输入用户名和密码,但您的 Git 密码以纯文本形式存储。
You can store your credentials using the following command
您可以使用以下命令存储您的凭据
git config credential.helper store
git push http://example.com/repo.git
# Now type username and password and it should be saved by git.
回答by hiandbaii
You can use the Git configuration inside the repository you are using. In ${local_repo_folder}/.git/config
, add your user details there. That way you can configure which user to use on a per-repository level.
您可以在正在使用的存储库中使用 Git 配置。在 中${local_repo_folder}/.git/config
,在那里添加您的用户详细信息。这样您就可以配置在每个存储库级别上使用哪个用户。
回答by Megha Sahu
Use this command to relogin with a different account on GitHub:
使用此命令在 GitHub 上使用其他帐户重新登录:
git remote set-url origin "remote repository URL"