使用git bash配置用户和密码

eee*_*jay 68 git credentials github

我在Windows 7上使用git bash.我们使用github作为我们的存储库源.

每次我推或拉我都必须提供用户和密码凭据.我知道我的SSH密钥设置正确,否则我将无法访问存储库.(也就是说,一旦我输入我的凭证,推/拉工作正常.)

我进去了

git config --global user.name myusername
git config --global user.email myemail
git config --global github.user myusername
git config --global github.token mytoken
Run Code Online (Sandbox Code Playgroud)

但是,尽管如此,每次推/拉时都要求我提供凭据.

man*_*lds 54

确保您使用ssh url作为github repo而不是https url.当您使用https而不是SSH时,它会询问用户名和密码.您可以检查文件.git/config或运行git config -egit remote show origin验证网址,并在需要时进行更改.

  • 由于内部防火墙规则我不能使用SSH(不要问......)还有另一种让git自动登录的方法吗? (13认同)

Ice*_*nte 44

从git bash我更喜欢运行命令:

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

此时运行命令git pull并输入您的凭证一次应将其存储以备将来使用.Git有一个内置的凭证系统,可以在不同的操作系统环境中运行.您可以在此处获取更多详细信息:https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

  • git config --global credential.helper 缓存 https://help.github.com/en/github/using-git/caching-your-github-password-in-git (4认同)
  • 是否有 Linux 替代方案,或者这也适用于 Linux? (2认同)

Che*_*胡晨海 17

对于那些使用访问令牌和Windows环境的人,有一种简单的方法可以做到这一点:

开始菜单→ 凭据管理器Windows凭据 →查找行(Git:https:// whatever / your-repository / url)→编辑, 用户名是PersonalAccessToken,密码是您的访问令牌。

  • 这对于设置要使用的值很有用,但要告诉 git 使用此管理器,请遵循 @IcedD​​ante 或 @notreadbyhumans 答案:`git config --global credential.helper wincred` (3认同)

小智 8

如果您是Mac用户并启用了钥匙串,则需要删除钥匙串中存储的授权信息:

- Open up Keychain access
- Click "All items" under category in the left-hand column
- Search for git
- Delete all git entries.
Run Code Online (Sandbox Code Playgroud)

然后,您应该使用git config以下命令从终端更改用户名和电子邮件:

$ git config --global user.name "Bob"

$ git config --global user.email "bob@example.com"
Run Code Online (Sandbox Code Playgroud)

现在,如果您尝试推送到存储库,将要求您输入用户名和密码。输入您要切换到的登录凭据。如果您使用其他用户名和密码或终端上先前切换的帐户在浏览器上登录GitHub,则通常会弹出此问题。


not*_*ans 6

对于 Windows 的 git bash,以下其他答案的组合对我有用(使用 GitHub 客户端即 https,而不是 ssh 检出存储库):

  1. 生成个人访问令牌
  2. 在你的 repo 中启动一个 git bash 会话
  3. git config --global credential.helper wincred
  4. git pull
  5. 将 PersonalAccessToken 作为用户名
  6. 将个人访问令牌作为密码


小智 6

添加远程为:

git remote add https://username:password@github.com/repodir/myrepo.git
Run Code Online (Sandbox Code Playgroud)

  • 现在是 git remote add <name> <url> (2认同)

小智 5

我在另一个答案中写了一个答案:How to change git account in Git bash?

不过,我也在这里分享。

更改用户名和电子邮件全局

git config --global user.name "<username>"
git config --global user.email "<email>"
Run Code Online (Sandbox Code Playgroud)

更改当前存储库的用户名和电子邮件

git config  user.name "<username>" --replace-all
git config  user.email "<email>" --replace-all
Run Code Online (Sandbox Code Playgroud)