繁体   English   中英

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

[英]How can I save username and password in Git?

我想在Git ExtensionsSourcetree或任何其他 Git GUI 中自动使用推拉,而无需每次都在提示中输入我的用户名和密码。

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

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

git config --global credential.helper store

然后

git pull

提供用户名和密码,稍后将记住这些详细信息。 凭据存储在磁盘上的文件中,具有“仅用户可读/可写”的磁盘权限,但仍以明文形式。

如果您想稍后更改密码

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

推荐和安全的方法:SSH

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

然后,如果私钥保存为id_rsa~/.ssh/目录中,我们添加它进行身份验证,如下所示:

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 将您的密码保存在内存中一段时间​​:

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

# 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 帮助

您可以编辑~/.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 或其他

请参阅文档

重启你的终端。

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

原版 DevOps Git 凭证和私有包备忘单

这些都是您可以安全地验证 Git 以克隆存储库而无需交互式密码提示的所有方法和工具。

  • SSH 公钥
    • SSH_ASKPASS
  • API 访问令牌
    • GIT_ASKPASS
    • .gitconfig 而不是
    • .gitconfig [凭证]
    • .git 凭证
    • .netrc
  • 私人套餐(免费)
  • Node.js / npm package.json
  • Python /pip/鸡蛋requirements.txt
  • 红宝石Gemfile
  • 去吧.mod

银弹

想要Just Works™? 这是神奇的银弹。

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

MY_GIT_TOKEN=xxxxxxxxxxxxxxxx

对于 GitHub,请逐字复制并运行这些行:

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”部分。

安全

请参阅备忘单的“安全”部分。

我认为缓存凭据更安全,而不是永远存储它:

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

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

它既好又安全。

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

只需将您的凭据放在 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

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

  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"

它仅影响该文件夹(因为您的配置是本地的)。

您可以使用 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 以将凭据存储在磁盘上

除了编辑~/.gitconfig文件之外,如果你从命令行调用,你可以这样做:

git config --local --edit

或者

git config --global --edit

在此处输入图像描述 在默认文本编辑器中编辑 git 配置文件

也可以使用命令行直接编辑 git config 文件(不用编辑器)

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'

请注意始终使用引号。 如果您使用双引号,您的用户名和密码可能会使用一些字符会破坏您的密码。

--local--global表示为项目或操作系统用户保存配置参数。

将用户名和密码存储在 .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 机器人非常有用,例如,通过为不同的分支设置规则,然后通过在 Docker Hub 中的post_push挂钩中推送到它来触发它,从而解决同一 Docker Hub 存储库中的链自动构建问题

在 Stack Overflow 上可以看到一个例子。

如果您使用 SSH 身份验证比用户名/密码身份验证更安全。

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

ssh-add -K ~/.ssh/id_rsa

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

在这种情况下,您需要 git credential helper 来告诉 Git 使用以下命令行记住您的 GitHub 密码和用户名:

git config --global credential.helper wincred

如果您正在使用使用 SSH 密钥的存储库,那么您需要 SSH 密钥进行身份验证。

以前的答案都不适合我。 每次我想fetchpull取时,我都会得到以下信息:

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


对于 Mac

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

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

对于 Windows

我能够使用此 Stack Exchange 帖子中的信息使其工作: 如何避免每次推送到 Bitbucket 时都被询问密码

只需使用

git config --global credential.helper store

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

截至 2021 年,HTTPS 远程有一个安全的、用户友好的跨平台解决方案。 无需再输入密码! 不再有 SSH 密钥! 不再有个人访问令牌!

安装 GitHub 开发的Git Credential Manager下载)。 它支持对 GitHub、BitBucket、Azure 和 GitLab 的无密码浏览器内 OAuth 身份验证。 这意味着您可以在 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 的高级用户可能更喜欢将凭证存储更改为 libsecret。

化妆品配置文档):

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

全局保存用户名和密码:

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

rofrol 的评论,在 Linux Ubuntu 上,从这个答案,这里是如何在 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 内置版本和自制版本都默认存在。

查看官方 Git 文档:

如果您使用 SSH 传输连接到遥控器,您可能拥有一个没有密码的密钥,这样您就可以安全地传输数据而无需输入用户名和密码。 然而,这对于 HTTP 协议是不可能的——每个连接都需要一个用户名和密码。 对于具有双因素身份验证的系统来说,这变得更加困难,因为您用于密码的令牌是随机生成的并且无法发音。

幸运的是,Git 有一个凭证系统可以帮助解决这个问题。 Git 在框中提供了一些选项:

  • 默认是根本不缓存。 每个连接都会提示您输入用户名和密码。

  • “缓存”模式将凭据保存在内存中一段时间​​。 没有任何密码存储在磁盘上,并且在 15 分钟后从缓存中清除。

  • “存储”模式将凭据保存到磁盘上的纯文本文件中,并且它们永不过期。 这意味着在您更改 Git 主机的密码之前,您无需再次输入您的凭据。 这种方法的缺点是您的密码以明文形式存储在主目录中的纯文本文件中。

  • 如果您使用的是 Mac,Git 带有“osxkeychain”模式,该模式将凭据缓存在附加到您的系统帐户的安全钥匙串中。 此方法将凭据存储在磁盘上,并且它们永不过期,但它们使用存储 HTTPS 证书和 Safari 自动填充的同一系统进行加密。

  • 如果您使用的是 Windows,则可以安装一个名为“Git Credential Manager for Windows”的帮助程序。 这类似于上面描述的“osxkeychain”帮助程序,但使用 Windows Credential Store 来控制敏感信息。 它可以在https://github.com/Microsoft/Git-Credential-Manager-for-Windows找到。

您可以通过设置 Git 配置值来选择其中一种方法:

git config --global credential.helper cache

git config --global credential.helper store

7.14 Git 工具 - 凭证存储

如果您在 Windows 上使用 Git Credential Manager ...

git config -l应该显示:

credential.helper=manager

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

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

如果你有代理并且你的 Git 服务器在内部网络上,还要确保你没有设置HTTP_PROXYHTTPS_PROXYNO_PROXY环境变量。

您还可以使用git-gui测试 Git fetch/push/pull,它链接到C:\Users\<username>\AppData\Local\Programs\Git\mingw64\libexec\git-core中的凭证管理器二进制文件

对于 Windows 用户,请查看.gitconfig文件并检查已为凭据帮助程序配置的内容。 如果您有以下...

[凭据“helperselector”] 选择 = wincred

您将在 Windows 凭据管理器中找到凭据。

在此处输入图像描述

在那里您可以编辑凭证。

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

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

因此,或者您可能希望重新配置 Git 以使用内置的 Git 凭证管理器......

git config --global credential.helper manager

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

如果您(像我一样)必须使用几种不同的用户名/密码组合处理来自多个提供商(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),通常可以使用“*”。 相反,如果您的存储库由不同的提供商托管,那么我建议为每个存储库显式设置指向提供商的链接,如下例所示(对于 GitLab):

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

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

如果 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

我个人不赞成使用global局域的git config ,因为这在多帐户情况下会一团糟。

GitHub 的推荐现在变了,最好的方法也是最简单的。 详情在这里

  1. 安装 Github 官方 cli 例如对于 mac: brew install gh
  2. 在终端中输入gh auth login ,然后按照提示进行操作。

要将您的用户名和用户密码保存到 github 帐户中,只需依次运行这些命令。

git config --global user.name "userName"
git config --global user.email "usernam@gmail.com"
git config --global user.password "userPassword"
git config --global credential.helper store
git config --list --show-origin

然后使用以下命令生成密钥:

ssh-keygen -t rsa -C "useremail@gmail.com"

注意:复制创建 id_rsa 文件的文件位置然后转到该文件位置 --> 打开 gitbash 或命令提示符 --> 运行命令 - cat id_rsa.pub

将显示 SSH 密钥,复制此 SSH 密钥并将其粘贴到您的 gihub/gitlab 帐户中

对于 Windows 用户,这种方式可以工作:

注意:如果您为 GitHub 启用了双因素身份验证,请暂时禁用它

  • 步骤1

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

  • 第2步

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

    在此处输入图像描述

  • 第 3 步 - 填写字段

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

    用户名:你的 GitHub 用户名

    密码:你的 GitHub 用户名

    在此处输入图像描述

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

双因素身份验证改变了用户对网站进行身份验证的方式,但 Git 仍然假定用户可以从 memory 输入密码。

引入git-credential-oauth :一个 Git 凭证助手,可以安全地验证到 GitHub、GitLab、BitBucket 和其他使用 OAuth 的伪造。

没有更多的密码! 没有更多的个人访问令牌! 没有更多的 SSH 键!

第一次推送时,助手会打开浏览器 window 进行身份验证。 缓存超时内的后续推送不需要交互。

https://github.com/hickford/git-credential-oauth/releases/安装

配置:

git config --global --unset-all credential.helper
git config --global --add credential.helper "cache --timeout 7200" # two hours
git config --global --add credential.helper oauth
git config --global user.name "your username"
git config --global user.password "your password"

查看

git config --list

对于有同样问题的 gitlab 用户 -

您可以设置部署令牌以克隆或从存储库中提取(您不能使用部署令牌将代码推送到存储库)。

Here you can know more about gitlab deploy tokens: https://docs.gitlab.com/ee/user/project/deploy_tokens/index.html

创建部署令牌后,使用以下内容克隆存储库:

git clone https://${username}:${deploy_token}@gitlab.com/yourusername/yourreponame.git

我认为这种方法比全局保存 git 用户名和 git 密码更好(例如,它在远程共享机器中可能不安全)

你的问题:

我想在 Git Extensions、Sourcetree 或任何其他 Git GUI 中自动使用推拉,而无需每次都在提示中输入我的用户名和密码。
那么如何将我的凭据保存在 Git 中呢?

如果您在 github 或其他提供商中,我建议不要将它们保存在 Git 中,例如~/.git-credentials ,但应将它们视为加密机密

将凭据设置为以下格式:

https://your_user_name:your_token@github.com/your_user_name/your_repo_name.git

把它作为加密的秘密,比如 secrets.REPOSITORY:

行动秘诀

然后您可以使用它来克隆公共或私人仓库及其子模块,以及自动进行推送和拉取

# put the credentials in a variable
export REPOSITORY=${{ secrets.REPOSITORY }}

# git clone public or private repos
git clone --recurse-submodules -j8 ${REPOSITORY}

# git pull will do automatic
cd /path/to/the/repo
git pull

# git push to a branch in the repo
git add . && \
  git commit -m "Action from ${GITHUB_SHA}" && \
  git push --force $REPOSITORY master:$BRANCH

你可以简单地添加.git-credentials

然后添加以下行:

git:https://:<令牌/密码>@gitlab.com

那是

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM