如何在 Git 中保存用户名和密码?

How can I save username and password in Git?

我想在 Git Extensions, Sourcetree 或任何其他 Git GUI 中自动使用推送和拉取,而无需在提示中输入我的用户名和密码,每次时间.

那么如何在 Git 中保存我的凭据?

注意:此方法将凭证以 plaintext 形式保存在您的 PC 磁盘上。您计算机上的每个人都可以访问它,例如恶意 NPM 模块。

运行

git config --global credential.helper store

然后

git pull

提供用户名和密码,稍后将记住这些详细信息。凭据存储在磁盘上的文件中,磁盘权限为“just user readable/writable”,但仍为明文。

如果以后要修改密码

git pull

会失败,因为密码不正确,git然后从~/.git-credentials文件中删除有问题的用户+密码,所以现在重新运行

git pull

提供一个新密码,以便它像以前一样工作。

您可以使用 git config 在 Git 中启用凭据存储。

git config --global credential.helper store

当运行此命令时,第一次从远程存储库拉取或推送时,系统会询问您的用户名和密码。

之后,为了与远程存储库进行后续通信,您不必提供用户名和密码。

存储格式为.git-credentials文件,明文存储。

此外,您可以为 git config credential.helper 使用其他助手,即内存缓存:

git config credential.helper 'cache --timeout=<timeout>'

它采用可选的 timeout parameter,确定凭据将在内存中保留多长时间。使用助手,凭据将永远不会接触磁盘,并将在指定的超时后被删除。 default 值为 900 秒(15 分钟)


警告:如果您使用此方法,您的Git帐户密码将以纯文本格式保存,在global .gitconfig file,例如在 Linux 中它将是 /home/[username]/.gitconfig

如果您不希望这样做,请为您的帐户使用 ssh key

打开凭据助手,这样 Git 会将您的密码保存在内存中一段时间​​:

在终端中,输入以下内容:

# Set Git to use the credential memory cache
git config --global credential.helper cache

默认情况下,Git 会将您的密码缓存 15 分钟。

要更改默认密码缓存超时,请输入以下内容:

# Set the cache to timeout after 1 hour (setting is in seconds)
git config --global credential.helper 'cache --timeout=3600'

来自 GitHub Help.

使用 SSH 身份验证比 username/password 身份验证更安全。

如果您使用的是 Mac,SSH 客户端身份验证已集成到 macOS 钥匙串中。创建 SSH 密钥后,在终端中输入:

ssh-add -K ~/.ssh/id_rsa

这会将 SSH 私钥添加到 macOS 钥匙串。 Git 客户端在连接到远程服务器时将使用 SSH。只要你在服务器上注册了你的 ssh public 密钥,你就可以了。

您可以编辑 ~/.gitconfig 文件以 存储 您的 凭据 :

sudo nano ~/.gitconfig

哪个应该已经有了

[user]
        email = your@email.com
        user = gitUSER

您应该在此文件的底部添加以下内容。

[credential]
        helper = store

我推荐这个选项的原因是因为它是全局的,如果在任何时候你需要删除你知道去哪里更改的选项。

仅在您的个人计算机上使用此选项。

然后拉的时候|克隆|输入你的Git密码,一般情况下,密码会以

格式保存在~/.git-credentials
https://gituser:gitpassword@domain.xxx

其中 DOMAIN.XXX 可以是 github.com、bitbucket.org 或其他

the documentation

重新启动您的终端。

只需将您的凭据放在 URL 中,如下所示:

https://Username`**:**`Password`**@**`github.com/myRepoDir/myRepo.git`

您可以这样存储它:

git remote add myrepo https://Userna...

...使用的示例:

git push myrepo master`

现在 列出 URL 别名:

git remote -v

...删除其中之一的命令:

git remote rm myrepo

推荐的安全方法:SSH

创建 SSH GitHub 密钥。转到 github.com设置SSH 和 GPG 密钥新 SSH 密钥。现在将您的私钥保存到您的计算机。

那么,如果私钥在~/.ssh/目录下保存为id_rsa,我们添加它用于身份验证:

ssh-add -K ~/.ssh/id_rsa

更安全的方法:缓存

我们可以使用git-credential-cache将我们的用户名和密码缓存一段时间。只需在您的 CLI(终端或命令提示符)中输入以下内容:

git config --global credential.helper cache

您也可以这样设置超时时间(以秒为单位):

git config --global credential.helper 'cache --timeout=3600'

您可以使用 git-credential-store 将未加密的密码存储在磁盘上,仅受文件系统权限的保护。

例子

git config credential.helper store
git push http://example.com/repo.git

Username: <type your username>
Password: <type your password>

[Several days later]

git push http://example.com/repo.git

[Your credentials are used automatically]

您可以检查存储在文件 ~/.git-credentials 中的凭据。

有关详细信息,请访问 git-credential-store - Helper to store credentials on disk

在这种情况下,您需要 git 凭据帮助程序告诉 Git 使用以下命令行记住您的 GitHub 密码和用户名:

git config --global credential.helper wincred

如果您使用 SSH 密钥使用存储库,则需要 SSH 密钥进行身份验证。

对于全局设置,打开终端(从任何地方),运行以下内容:

  git config --global user.name "your username"
  git config --global user.password "your password"

由此,您计算机上的任何本地 Git 存储库都将使用该信息。

您可以通过执行以下操作为每个存储库单独配置:

  • 在存储库文件夹中打开终端。

  • 运行 如下:

      git config user.name "your username"
      git config user.password "your password"
    

它只影响那个文件夹(因为你的配置是本地的)。

除了编辑 ~/.gitconfig 文件外,您还可以通过命令行调用:

git config --local --edit

git config --global --edit

正在默认文本编辑器中编辑 git 配置文件

您也可以使用命令行直接编辑 git 配置文件(无需编辑器)

git config --local user.name 'your username'
git config --local user.password 'your password'

git config --global user.name 'your username'
git config --global user.password 'your password'

Note to always use single quotes. Your username and password may use some characters that would break your password if you use double quotes.

--local--global 表示为项目或 OS 用户保存配置参数。

来自 , on Linux Ubuntu, from ,以下是 Ubuntu 上的操作方法:

sudo apt-get install libsecret-1-0 libsecret-1-dev
cd /usr/share/doc/git/contrib/credential/libsecret
sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret

其他一些发行版提供了二进制文件,因此您不必构建它。

在 OS X 中,它通常“构建”时添加了一个默认模块“osxkeychain”,因此您可以免费获得它。 OS X built-in 和自制软件都默认存在。

在浏览了 几十 个 Stack Overflow 帖子、博客等之后,我尝试了 every 方法,这就是我的方法想出了。它涵盖了一切

The vanilla DevOps Git credentials & private packages cheat sheet

这些是您可以安全地验证 Git 以克隆存储库 而无需交互式密码提示 .

的所有方法和工具
  • SSH public 密钥
    • SSH_ASKPASS
  • API 访问令牌
    • GIT_ASKPASS
    • .git配置而不是
    • .git配置[凭据]
    • .git-凭据
    • .netrc
  • 私人套餐(免费)
  • Node.js / npm package.json
  • Python / pip / eggs requirements.txt
  • Ruby gems 宝石文件
  • Go go.mod

银弹

想要 Just Works™?这就是灵丹妙药。

获取您的访问令牌(如果您需要 GitHub 或 Gitea 说明,请参阅作弊 sheet 部分)并将其设置在环境变量中(两者都是用于本地开发和部署):

MY_GIT_TOKEN=xxxxxxxxxxxxxxxx

对于 GitHub,复制并 运行 这些行 verbatim:

git config --global url."https://api:$MY_GIT_TOKEN@github.com/".insteadOf "https://github.com/"
git config --global url."https://ssh:$MY_GIT_TOKEN@github.com/".insteadOf "ssh://git@github.com/"
git config --global url."https://git:$MY_GIT_TOKEN@github.com/".insteadOf "git@github.com:"

恭喜。现在,任何自动工具克隆 Git 存储库都不会受到密码提示的阻碍,无论是使用 HTTPS 还是任何一种 SSH URL.

没有使用 GitHub?

对于其他平台(Gitea、GitHub 和 Bitbucket),只需更改 URL。不要更改用户名(虽然是任意的,但不同的配置条目需要它们)。

兼容性

这在 macOS、Linux、Windows(在 Bash)、Docker、CircleCI, Heroku, Akkeris 等地方有效

更多信息

参见作弊的“.gitconfig insteadOf”部分 sheet。

安全

参见作弊的“安全”部分 sheet。

None 之前的答案对我有用。每次我想要 fetchpull:

时,我都会不断收到以下信息

Enter passphrase for key '/Users/myusername/.ssh/id_rsa':


对于 Mac

我可以通过以下方式阻止它询问我的密码:

  1. 通过运行打开配置:vi ~/.ssh/config
  2. 添加了以下内容:UseKeychain yes
  3. 保存退出:按Esc,然后输入:wq!

对于Windows

我能够使用此 Stack Exchange post 中的信息让它工作: How to avoid being asked passphrase each time I push to Bitbucket

将用户名和密码存储在 .git-credentials

.git-credentials 是你的用户名和密码(访问令牌)存储的地方,当你 运行 git config --global credential.helper store,这是其他答案建议的,然后输入你的用户名和密码或访问令牌:

https://${username_or_access_token}:${password_or_access_token}@github.com

因此,为了保存用户名和密码(访问令牌):

git config —-global credential.helper store
echo “https://${username}:${password_or_access_token}@github.com“ > ~/.git-credentials

这对 GitHub 机器人非常有用,例如通过为不同的分支制定规则来解决 Chain automated builds in the same Docker Hub repository,然后通过在 Docker Hub 的 post_push 挂钩中推送它来触发它。

在 Stack Overflow here 上可以看到这方面的示例。

只需使用

git config --global credential.helper store

并执行 git 拉动 。它将要求输入用户名和密码。从现在开始,它不会提供任何用户名和密码提示。它将存储详细信息。

在完整阅读答案并试验了该问题的大部分答案后,我最终找到了适合我的过程。我想分享它,以防有人必须处理复杂的用例,但仍然不想像我一样浏览所有答案和 gitcredentials, gitcredentials-store 等手册页。

在下面找到我建议的程序 IF 你(像我一样)必须处理来自多个提供商的多个存储库(GitLab,GitHub , Bitbucket 等)使用几种不同的用户名/密码组合。如果您只有一个帐户可以使用,那么您最好使用 git config --global credential.helper storegit config --global user.name "your username" 等解决方案,这些解决方案在之前的答案中已经得到很好的解释。

我的解决方案:

  1. 取消设置全局凭证助手,以防以前的一些实验妨碍:)

    git config --global --unset credentials.helper
    
  2. 移动到你的 repo 的根目录并禁用本地凭证助手(如果需要)

    cd /path/to/my/repo
    git config --unset credential.helper`
    
  3. 创建一个文件以将您的存储库凭据存储到

    git config credential.helper 'store --file ~/.git_repo_credentials'
    

    注意:此命令会在您的主目录中创建一个名为“.git_repo_credentials”的新文件,Git 将您的凭据存储到该文件中。如果不指定文件名,Git 使用默认的“.git_credentials”。在这种情况下,只需发出以下命令即可:

    git config credential.helper store
    
  4. 设置您的用户名

    git config credential.*.username my_user_name
    

    注意:如果您的存储库来自同一提供商(例如 GitLab),通常可以使用“*”。相反,如果您的存储库由不同的提供商托管,那么我建议将 link 显式设置为每个存储库的提供商,如以下示例(对于 GitLab):

    git config credential.https://gitlab.com.username my_user_name
    

此时,如果您发出需要凭据的命令(例如git pull),您将被要求输入对应于“my_user_name”的密码。这只需要一次,因为 git 将凭据存储到“.git_repo_credentials”,并在后续访问时自动使用相同的数据。

我认为缓存凭据比永远存储它更安全:

git config --global credential.helper 'cache --timeout=10800'

现在您可以输入您的用户名和密码(git pull 或...),并在接下来的三个小时内继续使用 Git。

很好,也很安全。

超时的单位是秒(本例中是三小时)。

查看官方 Git 文档:

If you use the SSH transport for connecting to remotes, it’s possible for you to have a key without a passphrase, which allows you to securely transfer data without typing in your username and password. However, this isn’t possible with the HTTP protocols – every connection needs a username and password. This gets even harder for systems with two-factor authentication, where the token you use for a password is randomly generated and unpronounceable.

Fortunately, Git has a credentials system that can help with this. Git has a few options provided in the box:

  • The default is not to cache at all. Every connection will prompt you for your username and password.

  • The “cache” mode keeps credentials in memory for a certain period of time. None of the passwords are ever stored on disk, and they are purged from the cache after 15 minutes.

  • The “store” mode saves the credentials to a plain-text file on disk, and they never expire. This means that until you change your password for the Git host, you won’t ever have to type in your credentials again. The downside of this approach is that your passwords are stored in cleartext in a plain file in your home directory.

  • If you’re using a Mac, Git comes with an “osxkeychain” mode, which caches credentials in the secure keychain that’s attached to your system account. This method stores the credentials on disk, and they never expire, but they’re encrypted with the same system that stores HTTPS certificates and Safari auto-fills.

  • If you’re using Windows, you can install a helper called “Git Credential Manager for Windows.” This is similar to the “osxkeychain” helper described above, but uses the Windows Credential Store to control sensitive information. It can be found at https://github.com/Microsoft/Git-Credential-Manager-for-Windows.

You can choose one of these methods by setting a Git configuration value:

git config --global credential.helper cache

git config --global credential.helper store

来自7.14 Git Tools - Credential Storage

如果您在 Windows 上使用 Git 凭据管理器...

git config -l 应该显示:

credential.helper=manager

但是,如果系统没有提示您输入凭据,请按照以下步骤操作:

  1. 从“开始”菜单打开控制面板
  2. Select 用户帐户
  3. Select 在左侧菜单中管理您的凭据
  4. 删除与 Git 或 GitHub
  5. 相关的任何凭据

如果您有代理并且您的 Git 服务器在内网

您还可以使用 git-gui 测试 Git fetch/push/pull,它链接到 C:\Users\<username>\AppData\Local\Programs\Git\mingw64\libexec\git-core

中的凭证管理器二进制文件

对于 Windows 用户,请查看 .gitconfig 文件并检查为凭证助手配置的内容。如果你有以下...

[凭证“helperselector”] 选中 = wincred

您将在 Windows Credential Manager 中找到凭据。

您可以在此处编辑凭据。

注意:Wincred 已被弃用,请参阅...

https://github.com/Microsoft/Git-Credential-Manager-for-Windows#notice-this-project-is-no-longer-being-maintained-warning

因此,或者您可能需要重新配置 Git 以使用 built-in Git 凭据管理器...

git config --global credential.helper manager
git config --global user.name "your username"
git config --global user.password "your password"

检查

git config --list

对于 Windows 用户,这种方式可行:

注意:如果您为GitHub启用了two-factor authentication,请暂时禁用它

  • 步骤 1

    转到控制面板用户帐户凭据管理器Windows 凭据

  • 步骤 2

    转到通用凭据部分 → 添加通用凭据

  • 第 3 步 - 填写字段

    Internet 或网络地址:git.https://github.com

    用户名:您的GitHub用户名

    密码:您的GitHub用户名

    现在单击 确定。这会将您的 GitHub 帐户的密码和用户名保存到您的本地计算机

全局保存用户名和密码:

git config --global user.name "fname lname"
git config --global user.email "example@gmail.com"
git config --global user.password "secret"

获取特定设置,

git config --global --get user.name
git config --global --get user.email
git config --global --get user.password

正在获取所有 Git 设置:

git config --list --show-origin

如果 git 客户端不关心安全问题,请按以下方式编辑 url:

git remote set-url origin https://${access_token}@github.com/${someone}/${somerepo}.git

git clone 情况相同:

git clone https://${access_token}@github.com/${someone}/${somerepo}.git

我个人不赞成 git config 使用 global 域,因为这在多帐户情况下会很麻烦。

现在GitHub的建议变了,最好的方法也是最简单的。 Details here

  1. Install Github official cli。例如。 mac: brew install gh.
  2. 在您的终端中输入 gh auth login,然后按照提示操作。

截至 2021 年,有一个 安全 user-friendly cross-platform HTTPS 远程解决方案。无需再输入密码!没有更多的 SSH 密钥!没有更多的个人访问令牌!

安装Git Credential Manager developed by GitHub (downloads). It supports passwordless in-browser OAuth authentication to GitHub, BitBucket, Azure and GitLab. This means you can enable two-factor authentication on GitHub等平台,大大提高您账户的安全性。

推送时,您可以选择身份验证方法:

> git push
Select an authentication method for 'https://github.com/':
  1. Web browser (default)
  2. Device code
  3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...

在 Linux 上,需要进行一些设置。以下在内存中缓存凭据 20 小时,因此您每天最多需要进行一次身份验证。

git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions --timeout 72000

熟悉 gnome-keyring 或 KWallet 的高级用户可能更喜欢 change the credential store 而不是 libsecret。

化妆品配置 (docs):

  1. 更喜欢在终端而不是在 GUI 中选择身份验证方法(点击次数更少)
  2. 总是使用浏览器方法而不是每次都被询问(甚至更少的按键)
git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser

授权 GCM 访问您的 GitHub 帐户的屏幕截图(仅第一次看到):

后续身份验证的屏幕截图(每天显示一次)。无需点击。