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

2607

我想在 Git ExtensionsSourcetree 或其他 Git GUI 中自动使用 push 和 pull,无需每次手动输入用户名和密码。

那么我该如何保存我的 Git 凭据呢?


22
你也可以通过SSH进行身份验证:https://dev59.com/nmw15IYBdhLWcg3whMA1 - Anton Tarasenko
2
就GitHub而言,当前政策(截至2021年8月)是您不再能够使用用户名/密码。相反,您必须使用SSH或“个人访问令牌”:处理GitHub密码身份验证弃用 - paulsm4
3
@paulsm4 一种安全且用户友好的SSH或个人访问令牌的替代方案是通过Git凭据管理器使用OAuth,详见我的回答 https://dev59.com/PFsV5IYBdhLWcg3wrAZJ#71284566 - Colonel Panic
1
请参阅此2022 Git邮件列表线程 - VonC
33个回答

12

如果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,因为在多账户情况下会很混乱。

access_token是你可以在设置/开发者设置/个人访问令牌中生成的。记得将其授权为repo范围。


1
如果没有安全问题,就不需要凭据助手。 - Dominic Cerisano

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

[credential "helperselector"] selected = wincred

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

Enter image description here

在那里您可以编辑凭据。

注意: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

我在使用manager-core助手时遇到了问题,我的凭据每天都会消失 - 有什么线索可以解决这个问题吗? - Jonathan
修改是我正在寻找的答案,谢谢! - generic-user
@Jonathan 最近随 Git for Windows 捆绑的凭证管理器已经有了一些变化,所以最简单的方法就是下载最新的 Git for Windows 并在安装过程中选择凭证管理器。凭证管理器是一个独立的实用程序,你可以在 GitHub 上看到它:https://github.com/microsoft/Git-Credential-Manager-Core,但最好还是让 Git for Windows 安装程序来安装它,这样你就知道版本兼容性。 - Jason S
链接已损坏(404)。 - Peter Mortensen
这是在Windows上对我起作用的唯一命令,但是在运行它之后,我不得不执行'git fetch'和'git pull origin main'才能使Windows凭证窗口弹出。看起来被缓存了。 - heyjon

10

根据rofrol的评论,在Linux 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内置的和homebrew版本的都默认有此功能。


9
阅读完整的答案并尝试了大多数答案后,我最终找到了适合我的过程。我想分享一下,以防有人需要处理复杂的用例,但仍不想像我一样阅读所有答案和gitcredentialsgitcredentials-store等手册。
如果您(像我一样)必须处理来自几个提供者(GitLab、GitHub、Bitbucket等)的多个存储库,并使用几个不同的用户名/密码组合,则请按照以下步骤进行操作。如果您只有一个帐户要使用,则最好使用先前答案中非常好解释的git config --global credential.helper storegit config --global user.name "your username"等解决方案。
我的解决方案:
  1. Unset global credentials helper, in case some former experimentation gets in the way :)

    git config --global --unset credentials.helper
    
  2. Move to the root directory of your repo and disable the local credential helper (if needed)

    cd /path/to/my/repo
    git config --unset credential.helper`
    
  3. Create a file to store your repo's credentials into

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

    Note: this command creates a new file named ".git_repo_credentials" into your home directory, to which Git stores your credentials. If you do not specify a file name, Git uses the default ".git_credentials". In this case simply issuing the following command will do:

    git config credential.helper store
    
  4. set your username

    git config credential.*.username my_user_name
    

    Note: using "*" is usually ok if your repositories are from the same provider (e.g. GitLab). If instead your repositories are hosted by different providers then I suggest to explicitly set the link to the provider for every repository, like in the following example (for GitLab):

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

只是需要注意,当使用 * 时,我收到了以下警告:“警告:url没有方案:致命错误:无法解析凭据url:”。因此,我必须指定“credential.https://github.com.username”。 - Tonsic
在我的上一个评论中,stackoverflow评论将https://github...替换为github... - Tonsic
在单个计算机上与多个 Git 提供商一起工作的最佳灵活选项。谢谢。如果有人希望通过使用 SSH 密钥来实现这种行为,请检查您的 ~/.ssh/config 文件,并添加自定义的 SSH 密钥以用于每个 Git 服务器提供商。 - danipenaperez

7
如果您正在 Windows 上使用 Git 凭据管理器...
`git config -l` 应该显示:
credential.helper=manager

如果您没有收到凭据提示,请按照以下步骤操作:

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

还要确保您未设置HTTP_PROXYHTTPS_PROXYNO_PROXY环境变量,如果您使用代理并且您的 Git 服务器位于内部网络。

您还可以使用链接到凭证管理器二进制文件的C:\Users\\AppData\Local\Programs\Git\mingw64\libexec\git-core中的git-gui测试 Git 的获取/推送/拉取功能。


6

GitHub的推荐方法已经改变,而且最好的方法也是最简单的。 详情请见此处

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

4
两步验证已经改变了用户认证网站的方式,但是 Git 仍然假定用户可以从记忆中输入密码。
介绍 git-credential-oauth:一个 Git 凭据助手,使用 OAuth 安全地认证到 GitHub、GitLab、BitBucket 和其他锻造工具。

不再需要密码!不再需要个人访问令牌!不再需要 SSH 密钥!

第一次推送时,该助手将打开浏览器窗口以进行身份验证。在缓存超时内的后续推送不需要任何交互。

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

1
非常好:确实是Linux上GCM的一个很好的替代品。 - VonC
git: 'credential-oauth' is not a git command. See 'git --help'. - DollarAkshay
@DollarAkshay请确保git-credential-oauth二进制文件在PATH中,并且可执行。 - Colonel Panic

3
对于 Windows 用户,这种方法有效:
注意:如果您已为 GitHub 启用了两步验证,请暂时禁用它。
步骤1:前往“控制面板”→“用户帐户”→“凭据管理器”→“Windows 凭据”。
步骤2:转到“通用凭据”部分→“添加通用凭据”。
步骤3:填写字段。Internet 或网络地址:git.https://github.com,用户名:您的 GitHub 用户名,密码:您的 GitHub 用户名。现在点击“确定”。这将保存您的 GitHub 帐户的密码和用户名到本地计算机。

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

检查

git config --list

2
请注意,这个答案指出这是一种不安全的做法。 - Eric Aya
什么是安全的方式?请分享一些安全的提示,Eric Aya。 - Keshav Gera

2
你的问题:
我想在Git Extensions、Sourcetree或其他Git GUI中自动使用push和pull,而不需要每次在提示框中输入我的用户名和密码。那么,我该如何保存我的Git凭据?
如果你在GitHub或其他提供商,请不要将它们保存在Git中,例如~/.git-credentials,而应将它们视为加密的秘密
将凭据放入以下格式:

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

将其作为加密的秘密,例如secrets.REPOSITORY:

Actionsecrets

然后您可以使用它来克隆公共或私有存储库以及其子模块,并自动执行推送和拉取操作。

# 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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接