【问题标题】:Git asks for username every time I push每次我推送时,Git 都会询问用户名
【发布时间】:2012-07-09 08:16:24
【问题描述】:

每当我尝试进入我的 repo 时,git 都会要求 username & password

我每次重新输入密码都没有问题,但问题在于输入用户名。我使用https 来克隆我的存储库。

那么,我该如何配置 git,使其不会在每个 git push 上要求 username

我是 linux 新手,但 Windows git push 中的 IIRC 只要求输入密码。

【问题讨论】:

标签: linux git github


【解决方案1】:

自 2021 年 8 月 13 日起,不再接受基于密码的登录。您现在需要使用基于令牌/SSH 的身份验证。

如果您不想每次推送时都被询问,请安装GitHub CLI,然后缓存您的凭据:

git config --global credential.helper store
gh auth login   

并按照提示进行操作。但请务必检查 Authenticate Git with your GitHub credentials?Y

【讨论】:

    【解决方案2】:

    note,从 2021 年 8 月 13 日开始,github 在验证 Git 操作时将不再接受帐户密码,并将要求对 GitHub 上所有经过验证的 Git 操作使用基于令牌的身份验证.com。您也可以继续使用您喜欢的 SSH 密钥。

    因此,您必须使用给定身份验证上下文的用户名静态配置(使用 github 时):

    https://username:<personal-access-tokens>@repository-url.com
    

    请注意,我们使用的是&lt;personal-access-tokens&gt;,而不是&lt;password&gt;

    有关如何创建个人访问令牌的更多详细信息,请查看documentation

    github documentation 说:

    GitHub over HTTPS...每次使用 Git 进行身份验证时 GitHub,系统会提示您输入凭据以进行身份​​验证 使用 GitHub,除非您使用 credential helper 缓存它们。

    ...

    使用 SSH ... 每次使用 Git 向 GitHub 进行身份验证时, 系统会提示您输入 SSH 密钥密码,除非您已经 stored the key.

    github recommends 通过 HTTPS 连接。可能是因为他们want you to useGit Credential Manager Core。但是Git Credential Manager Core 在 Windows 和 macOS 上运行。 Linux 支持(截至发布时间)处于早期预览

    所以,我只会给出 Git 和 SSH 的解决方案。

    Git 的 documentation 讨论如何避免使用 gitcredentials 一遍又一遍地输入相同的凭据。

    解决方案 1

    使用凭证助手缓存密码(在内存中短时间)。

    git config --global credential.helper cache
    

    解决方案 2

    使用凭证助手存储密码(无限期地存储在磁盘上)。

    git config --global credential.helper 'store --file ~/.my-credentials'
    

    您可以在documentation 中找到凭据的保存位置(如果未使用 --file 明确设置)。

    注意: 为了解决 gitcredentials 存储的凭据完全未加密(“原样”)的concern,您始终可以在使用前加密文件并解密。

    解决方案 3

    这几乎是来自Generating a new SSH key and adding it to the ssh-agent 的直接复制粘贴。我认为跟随链接会比从这里阅读更好。无论如何,我将在这里复制粘贴命令。

    1. 使用ssh-keygen -t ed25519 -C "your_email@example.com" 创建 ssh 密钥。您还可以使用 RSA 代替 Ed25519,使用 ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

    2. Add the SSH key to your account on GitHub.

    3. 如果没有启动,使用$ eval "$(ssh-agent -s)"在后台启动ssh-agent

    4. 使用$ ssh-add ~/.ssh/id_ed25519 将您的 SSH 私钥添加到 ssh-agent。如果您使用不同的名称创建了密钥,或者如果您要添加具有不同名称的现有密钥,请将命令中的 id_ed25519 替换为您的私钥文件的名称。

    【讨论】:

      【解决方案3】:

      使用以下命令可以给你 15 分钟的超时时间。

      $ git config --global credential.helper cache
      

      您也可以通过以下命令修改时间。这是 1 小时的示例。

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

      如果你想在本地缓存,使用命令一小时超时。

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

      【讨论】:

        【解决方案4】:
        1. 打开 Windows 凭据管理器并从通用凭据中删除 git:https://dev.azure.com/orgname。有关步骤,请查看Why TFS with GIT is not working from command line?
        2. 从文件夹C:\Users\{username} 中删除.git-credentials 文件。
        3. 确保从https://git-scm.com/download/win 更新git 最新版本

        【讨论】:

          【解决方案5】:

          我尝试了这些步骤并且成功了:

          1. 创建个性化访问令牌:https://docs.github.com/en/github/authenticating-to-github/keeping-your-account-and-data-secure/creating-a-personal-access-token

          2. 将令牌添加到 Android Studio (文件->设置->版本控制->Github->添加-使用令牌登录)

          3. 转到文件->设置->版本控制->Git 并取消选中'使用凭据助手'

          【讨论】:

            【解决方案6】:

            设置全天 24 小时的凭据。

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

            否则 1 小时将 86400 秒替换为 3600。

            “缓存”身份验证助手的所有配置选项:

            git help credential-cache

            更多信息:https://docs.github.com/en/get-started/getting-started-with-git/caching-your-github-credentials-in-git

            【讨论】:

              【解决方案7】:

              解决的正确方法是将http改为ssh

              您可以使用此git remote rm origin 删除您的远程仓库。

              然后您可以使用 ssh sintax 再次添加它(您可以从 github 复制): git remote add origin git@github.com:username/reponame.git.

              查看这篇文章。 https://www.freecodecamp.org/news/how-to-fix-git-always-asking-for-user-credentials/

              【讨论】:

              • 嗯——没有。这是一种可能性,而不是“正确的方式”。还要检查this answer 中的 OP 评论(顺便说一句,这与您的非常相似)。
              【解决方案8】:

              我今天遇到了这个问题。如果您在 2020 年 11 月遇到此问题,那么您需要更新您的 git 版本。我收到一封电子邮件,告诉我这件事。 这是 github 团队的电子邮件。

              我们检测到您最近尝试向 GitHub 进行身份验证 使用旧版本的 Windows 版 Git。 GitHub 改变了方式 用户在使用 Git for Windows 时进行身份验证,现在需要 使用 Web 浏览器向 GitHub 进行身份验证。为了能够登录 通过网络浏览器,用户需要更新到最新版本的 Git 视窗。您可以在以下网址下载最新版本:

              如果您无法将适用于 Windows 的 Git 更新到最新版本,请参阅 以下链接了解更多信息和建议的解决方法:

              如果您对这些更改有任何疑问或需要任何 帮助,请联系 GitHub 支持,我们很乐意 进一步协助。

              感谢 GitHub 团队

              【讨论】:

                【解决方案9】:

                我使用将我的 Git 存储库用户名和密码存储在磁盘上。所以每次推送代码时git都不会要求输入用户名和密码

                按照这些命令将凭据保存在本地存储中

                $ git config credential.helper store
                or
                $ git config --global credential.helper store
                

                从现在开始,当第一次访问时,Git 会将凭据写入每个 URL 上下文的 ~/.git-credentials 文件。要查看这个文件的内容,可以使用如图所示的cat命令。

                $ cat  ~/.git-credentials
                

                【讨论】:

                  【解决方案10】:

                  当使用 HTTPS 而不是 SSH 下载时会发生这种情况,我实现的最简单的方法是我推送所有内容,因为我做了一些更改,其中一次它要求输入用户名和密码,然后我从我的机器和 git 中删除了目录克隆 SSH 地址。解决了。​​

                  只需使用 SSH 而不是 HTTP 克隆它,它不会询问用户名或密码。

                  即使您使用 SSH 下载,双重身份验证也会产生问题,因此禁用它可以解决问题。

                  【讨论】:

                    【解决方案11】:
                    git config --global credential.helper cache
                    

                    【讨论】:

                    • 这是 Linux 上更好的答案。
                    【解决方案12】:

                    对我来说,当我为我的帐户启用 2 因素身份验证 (2FA) 时,就会出现此问题。我会输入正确的凭据,但身份验证仍然会失败,因为终端身份验证当然不需要验证码。

                    我只需要为我的帐户禁用 2FA。这样做后,我必须在git push 期间只输入一次我的凭据。从那时起就不再需要它们了。

                    【讨论】:

                      【解决方案13】:

                      (仅适用于 Mac 操作系统)

                      对于 WindowsLinux,请参阅下面提到的 Github 文档链接。

                      来自 Github 文档:Documentation Link

                      在 Git 中缓存你的 GitHub 密码

                      • 您需要 Git 1.7.10 或更新版本才能使用 osxkeychain 凭证 帮手。

                        检查您的 Git 版本:git --version

                      • 找出 osxkeychain helper 已经安装:

                        git credential-osxkeychain

                        如果您收到以下响应,则说明您已经安装并可以在您的 Mac 上使用:

                        &gt; Usage: git credential-osxkeychain &lt;get|store|erase&gt;

                        如果您没有收到如上所示的响应,那么您可以运行以下命令安装它:

                        git credential-osxkeychain

                      • 告诉 git 全局使用 osxkeychain 获取 git 凭据:

                        git config --global credential.helper osxkeychain

                        它会在将凭据保存到钥匙串之前提示一次。

                      【讨论】:

                        【解决方案14】:

                        ssh + key authenticationhttps + credential.helper 更可靠

                        您可以将所有存储库配置为使用 SSH 而不是 HTTPS,如下所示:

                        git config --global url.ssh://git@github.com/.insteadOf https://github.com/
                        

                        url.&lt;base&gt;.insteadOf 记录在 here

                        【讨论】:

                          【解决方案15】:

                          更改克隆的 repo 工作:

                          git remote set-url origin git@github.com:gitusername/projectname.git
                          

                          使用缓存的凭据是可行的,设置超时时间可以让事情变得不那么烦人。如果您使用 Windows 凭据助手,那应该是最简单的方法(尤其是在 SSH 被阻止的情况下)。

                          【讨论】:

                          • 这是 GitHub 的正确答案(这个问题是专门问的......(不确定你为什么被否决)。
                          【解决方案16】:

                          编辑(由版主和 cmets 建议的 @dk14)

                          警告:如果您使用答案中的credential.helper store,您的密码将完全未加密(“原样”)存储在~/.git-credentials。请参阅下面的 cmets 部分或“已链接”部分的答案,尤其是在您的雇主对安全问题零容忍的情况下。

                          即使被接受,它也不能回答实际 OP 关于仅省略用户名(而不是密码)的问题。对于有这个确切问题的读者@grawity 的answer 可能会派上用场。


                          原答案(@Alexander Zhu):

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

                          $ git config credential.helper store
                          $ git push http://example.com/repo.git
                          Username: <type your username>
                          Password: <type your password>
                          

                          另外我建议你阅读
                          $ git help credentials

                          【讨论】:

                          • 警告,此方法没有安全性。您的密码以明文形式存储。
                          • 你可以在这里找到更好的方法来实现安全 - stackoverflow.com/a/28104587/1809978
                          • @MarkLakata - 请告诉我如何撤消此答案中的命令?撤消后,我将使用安全方法存储凭据。
                          • @testerjoe2 试试git config --unset credential.helper
                          • 不要忘记设置超时时间,默认为 15 分钟 - 将值设置为 3000 万,大约有 1 年。在这里找到:stackoverflow.com/a/35942890/1579667
                          【解决方案17】:

                          快速方法

                          在工作目录中获取 git 的 url:

                          git config remote.origin.url
                          

                          然后:

                          git config credential.helper store
                          
                          git push "url of your git"
                          

                          最后一次会询问用户名和密码

                          完成。

                          【讨论】:

                          • 谢谢,兄弟,你拯救了我的一天!
                          【解决方案18】:

                          你可以运行

                          git config --global credential.helper wincred
                          

                          在系统中安装并登录 GIT for windows 之后。

                          【讨论】:

                          • 在查询中没有看到 linux 单词。我的坏
                          【解决方案19】:

                          最简单的方法是创建一个~/.netrc 文件,内容如下:

                          machine github.com
                          login YOUR_GITHUB_USERNAME
                          password YOUR_GITHUB_PASSWORD
                          

                          (如图所示:https://gist.github.com/ahoward/2885020

                          您甚至可以关闭此文件的权限,这样任何人都无法通过键入以下内容读取您的密码:

                          chmod 600 ~/.netrc
                          

                          【讨论】:

                            【解决方案20】:

                            使用 Git 存储库进行永久身份验证

                            运行以下命令以启用凭据缓存:

                            $ git config credential.helper store
                            $ git push https://github.com/repo.git
                            
                            Username for 'https://github.com': <USERNAME>
                            Password for 'https://USERNAME@github.com': <PASSWORD>
                            

                            使用时还应指定缓存过期

                            git config --global credential.helper "cache --timeout 7200"
                            

                            启用凭证缓存后,将缓存 7200 秒(2 小时)


                            读取凭据文档

                            $ git help credentials
                            

                            【讨论】:

                            • 我将超时设置为 7200。但后来我想将其设置为 5 进行测试。所以我将它设置为 5,但是当我在 30 秒后再次尝试推送时,却没有要求我输入用户名或密码。
                            • 可能是您的凭证未在全球范围内存储。
                            • 关于安全性,值得参考文档:“此命令将凭据缓存在内存中以供将来的 Git 程序使用。存储的凭据永远不会接触磁盘,并且在可配置的超时后被遗忘。” git-scm.com/docs/git-credential-cache
                            • 不是git config credntial.helper store 那个危险的命令,它负责将明文凭据存储在 ~/.git-credentials 文件中吗?我认为(根据我的经验) ...cache...-command 就足够了:git config --clogal credential.helper 'cache --timeout 7200' 它创建了目录:~/.git-credential-cache,其中包含缓存文件所以,只需执行此命令而不是“。 ..store"-命令
                            • 对我不起作用。每次还是要密码
                            【解决方案21】:

                            当我只 git pull 时, git pull origin xxx, git 要求用户名和密码。

                            git config credential.helper store
                            

                            它有效。

                            【讨论】:

                              【解决方案22】:

                              我发现最简单的方法是使用这个命令:

                              git config --global credential.https://github.com.username <your_username>
                              

                              这适用于逐个站点并修改您的全局 git 配置。

                              要查看更改,请使用:

                              git config --global --edit
                              

                              【讨论】:

                              • 出于某种原因,这是唯一对我有用的方法。谢谢!
                              • 这种方法安全吗? github密码是不是只存本地电脑,不推送到github?
                              • 我不知道为什么这不是批准的答案,该命令完美运行并且完全响应并且仅针对问题:存储用户名,而不是密码。谢谢本!
                              • 你也可以运行:[ git config --global credential.helper 'cache --timeout=3600' ] 这样你的密码每次都会保存一个小时
                              • 这是我一直在寻找的答案。谢谢。我想明确输入密码(这样我就不会不小心 git push),但不是用户名。
                              【解决方案23】:

                              为避免输入用户名,但仍会提示输入密码,您可以简单地克隆包含用户名的存储库:

                              git clone user_name@example.gitrepo.com/my_repo.git
                              

                              【讨论】:

                                【解决方案24】:

                                除了 user701648 的回答之外,您还可以使用以下命令将凭据存储在主文件夹(所有项目的全局文件夹)中,而不是项目文件夹中

                                $ git config --global credential.helper store
                                $ git push http://example.com/repo.git
                                Username: <type your username>
                                Password: <type your password>
                                

                                【讨论】:

                                  【解决方案25】:
                                  1. $ git config credential.helper store

                                  2. $ git push/push https://github.com/xxx.git

                                  3. 然后输入你的用户名和密码。

                                  4. 完成

                                  【讨论】:

                                  • 不是git push/push https://github.com/xxx.git,应该是git push https://github.com/xxx.git
                                  【解决方案26】:

                                  如果你使用 SSH 版本,密码不会有任何问题。只有一次生成 SSH 密钥,告诉 git,this pc 将使用 this github 帐户,并且不再询问我任何访问权限(对于这台 pc)。

                                  How To Generate SSH for Github

                                  【讨论】:

                                    【解决方案27】:

                                    按照this article on GitHub 中的说明添加新的 SSH 密钥。

                                    如果 Git 仍然要求您输入用户名和密码,请尝试将远程 URL 中的 https://github.com/ 更改为 git@github.com:

                                    $ git config remote.origin.url 
                                    https://github.com/dir/repo.git
                                    
                                    $ git config remote.origin.url "git@github.com:dir/repo.git"
                                    

                                    【讨论】:

                                    • 这应该是正确的答案!保存您的凭证是非常不安全的。如果我有一个 SSH 密钥,它只会询问我的凭据,因为我没有此处列出的正确 URL。
                                    • 或者你可以使用git config remote.origin.url "ssh://git@yourRepoHost/yourRepoName.git"
                                    • git config remote.origin.url git@gitlab.com:adammajewski/ray-backward-iteration.git
                                    • 我必须记住使用 SSH URL 而不是 HTTP 进行克隆。节省了额外的步骤。
                                    • git@github URL 有效,但我还必须创建个人访问令牌和 SSH 密钥才能使我的身份验证正常工作
                                    【解决方案28】:

                                    您可以在本地存储库的.git/config 文件中完成此操作。该文件包含一个名为“remote”的部分,其中包含一个名为“url”的条目。 “url”条目应包含您正在谈论的存储库的 https 链接。

                                    当您在主机“url”前加上您的用户名时,git 不应再询问您的用户名。这是一个例子:

                                    url = https://username@repository-url.com
                                    

                                    【讨论】:

                                    • 如果您在用户名或密码中使用特殊字符,那么您应该转义它们。有关正确的转义字符,请参见 en.wikipedia.org/wiki/Percent-escape。例如 "!@#" [不带引号] 将替换为 "%21%40%23"。另请参阅下面的“Daniel Lerch”答案
                                    • 在同一个仓库工作的其他人会看到我的用户名吗?
                                    • 这应该是 OP 问题的正确答案,即“如何保留用户名,而不是密码”。这个答案不仅是保留用户名的正确方法,而且还避免了git config credential.helper store 可能存在的安全风险。
                                    • 我很感谢这个答案并尝试了这种方式,但它仍然每次都会问我(v 2.1.4) - 尝试使用 name@gitlab.com 和 name%40mailaddress.com@ 的 remoteurl gitlab.com - 它总是要求username for https://gitlab.com:
                                    • 我试过了,得到了unable to access 'https://&lt;me&gt;@bitbucket.org:&lt;x&gt;/&lt;y&gt;.git/': Illegal port number
                                    【解决方案29】:

                                    您可以通过在您的 Git 配置文件中添加如下内容来为给定站点的所有存储库设置您的用户名。您需要将“https://example.com”和“me”更改为实际 URL 和您的实际用户名。

                                    [credential "https://example.com"]
                                        username = me
                                    

                                    (这直接来自“git help credentials”)

                                    【讨论】:

                                      【解决方案30】:

                                      如果您使用 https 而不是 ssh,您可以按照 user701648 的说明在 .git/config 中编辑“url”,但如果您愿意,还可以添加密码:

                                      url = https://username:password@repository-url.com
                                      

                                      【讨论】:

                                      • 如果我的密码中包含@ 怎么办? url = https://username:abc@123@repository-url.com Could not resolve host: @123@git.com 给我的错误
                                      • 当服务器重定向(例如从 FOO 到 FOO.git)时,username 部分似乎被忽略了。
                                      猜你喜欢
                                      • 1970-01-01
                                      • 2012-06-13
                                      • 2021-01-27
                                      • 1970-01-01
                                      • 2014-11-02
                                      • 2012-04-02
                                      • 2023-04-10
                                      • 2021-04-07
                                      • 2013-07-29
                                      相关资源
                                      最近更新 更多