从多个帐户的命令行登录到github

MrE*_*MrE 4 git github

我开了一个新的GitHub帐户来分隔我的商业和个人回购.

现在,我是git init我当地的回购和git add remote origin <the repository HTTPS URL>

我尝试推送,它似乎总是采用我原始帐户的凭据,而不是提示我输入新帐户的凭据.

我尝试使用url格式

https://<username>:<password>@github.com/<username>/<repository.git>
Run Code Online (Sandbox Code Playgroud)

但这似乎没有帮助:我仍然收到一个错误,即凭据对原始帐户用户名无效.

如何使用多组凭据登录,或者如何在推送时以某种方式重置原始凭据以强制密码提示?

编辑:

我现在设法推动的唯一方法是在中指定username:password@github.com/urlgit push

小智 11

在 Unix 或 Mac 上使用 SSH 凭据

  1. 在工作区中配置 Git
  2. 制作/检查 SSH 凭据
  3. 更新在线 Github 设置

步骤 1. 在工作区中配置 Git

  1. 在您的 shell 中,添加您的用户名:git config --global user.name "your_username"

  2. 添加您的电子邮件地址:git config --global user.email "your_email_address@example.com"

  3. 要检查配置,请运行:git config --global --list

第 2 步:制作 SSH 凭证

  1. 打开 bash 或终端。

  2. cd ~/.ssh

  3. ls

  4. 应该有这两个文件:id_rsa 和 id_rsa.pub。这些是登录凭据。

  5. 如果 id_rsa.pub 中的电子邮件与您要使用的电子邮件不匹配,请创建新凭据。为了简单起见,请使用“id_rsa”作为保存密钥的文件并创建一个密码以供以后保存 ssh-keygen -t rsa -C "your_email@example.com"

  6. 将您的密码保存在安全的地方,以便在推送时使用,否则您将需要再次执行此操作。

第 3 步:更新在线 Github 设置

  1. vim ~/.ssh/id_rsa.pub

  2. 复制 id_rsa.pub 的内容并将其粘贴到 GitHub网站的帐户设置 > SSH 和 GPG 密钥下。完全按照显示的样子复制所有内容,没有多余的空格或行。为这些凭据指定一个名称,以提醒您它来自哪台计算机。

  3. 再次尝试git Push

故障排除

如果您收到有关权限过于开放的警告,Permissions 0644请尝试以下操作:

  • 设置私钥的权限:chmod 400 ~/.ssh/id_rsa
  • 确保~/.ssh/config指向正确的私钥

参考


MrE*_*MrE 10

我设法推送的唯一方法是username:password@github.com/在 Git push 命令的 URL 中指定。

  • 如果我的密码碰巧有@符号怎么办? (5认同)

Cod*_*ard 8

git config --global credential.helper cache
Run Code Online (Sandbox Code Playgroud)

...告诉git将密码缓存在内存中(默认情况下)为15分钟.您可以设置更长的超时:

git config --global credential.helper "cache --timeout=3600"
Run Code Online (Sandbox Code Playgroud)

更有用的链接:

https://confluence.atlassian.com/bitbucketserver/permanently-authenticating-with-git-repositories-776639846.html

使用.netrc文件.netrc文件是一种允许您指定要用于哪个服务器的凭据的机制.

这种方法允许您在每次从Git推送或拔出时都避免输入用户名和密码,但您的Git密码以纯文本格式存储.


您可以使用以下命令存储凭据

git config credential.helper store
git push http://example.com/repo.git
# Now type username and password and it should be saved by git.
Run Code Online (Sandbox Code Playgroud)