首页 文章

Git一直提示我输入密码

提问于
浏览
527

我一直在使用Git一段时间,但是不断要求输入密码开始让我感到困惑 .

我'm using OSX and Github, and I set up Git and my SSH keys as instructed by GitHub' s Set Up Git page . 我还在我的Mac OSX钥匙串中添加了github SSH密钥,如GitHub's SSH key passphrases page所述 . 我的公钥是在Git注册的 .

然而,每次我尝试git pull时,我都要输入我的用户名和密码 . 我需要为此设置除SSH密钥以外的其他东西吗?

21 回答

  • 33

    我想你解决了你的问题,但是我没有看到这里的解决方案对我有所帮助,所以在这里 .

    输入终端:

    echo "" > ~/.ssh/known_hosts
    

    这将清空您的 known_hosts 文件,并且您必须添加您使用并连接的每个主机,但它解决了问题 .

  • 495

    配置credential.helper

    在OS X(现在是macOS)上,在终端中运行它

    git config --global credential.helper osxkeychain
    

    它使git能够使用Keychain.app存储用户名和密码,并从密钥链中将密码短语检索到私有ssh密钥 .

    对于Windows使用:

    git config --global credential.helper wincred
    

    故障排除

    如果正确配置了git凭证帮助程序,macOS会将密码保存在密钥链中 . 有时connection between ssh and the passphrases stored in the keychain会破裂 . 运行 ssh-add -Kssh-add ~/.ssh/id_rsa 以再次将密钥添加到钥匙串 .

    macOS 10.12 Sierra改为ssh

    对于macOS 10.12,需要在每次重启后运行Sierra ssh-add -K . 要避免这种情况,请使用此内容创建 ~/.ssh/config .

    Host *
       AddKeysToAgent yes
       UseKeychain yes
       IdentityFile ~/.ssh/id_rsa
    

    从10.12.2的 ssh_config man 页面:

    UseKeychain在macOS上,指定系统在尝试使用特定键时是否应在用户的钥匙串中搜索密码 . 当用户提供密码短语时,此选项还指定密码短语一旦被验证为正确,是否应存储在密钥链中 . 论证必须是“是”或“否” . 默认为“否” .

    Apple已经添加了Technote 2449,它解释了发生了什么 .

    在macOS Sierra之前,ssh会出现一个询问您密码的对话框,并提供将其存储到钥匙串中的选项 . 此UI已在不久前被弃用,已被删除 .

  • 1

    正如其他人所说,您可以安装密码缓存助手 . 我大多只是想发布其他平台的链接,而不仅仅是mac . 我正在运行Linux服务器,这很有帮助:https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux

    对于mac:

    git credential-osxkeychain
    

    视窗:

    git config --global credential.helper wincred
    

    Linux的:

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

    还要找谁问你的密码短语 . 是git还是你的SSH代理?

    就我而言,每次我做 git pull 时都会问我:

    Enter passphrase for key '/work/username/.ssh/id_rsa':

    所以我认为是git要求密码 . 所以我一直在寻找解决方案,后来才意识到我的SSH代理已经关闭了 . 哪个可以使用 eval 'ssh-agent -s'ssh-add 修复为here.

    这是我犯的一个非常愚蠢的错误,但是在这里发布它,以防它有助于某人节省一些时间来咆哮错误的树,就像我做的那样 .

  • 3

    在使用github密钥之前,请按照教程Testing your SSH connection中的此步骤操作:

    $ ssh -T git@github.com
    # Attempts to ssh to GitHub
    
  • 9

    根据您的配置,有不同类型的身份验证 . 这里有几个:

    • git credential-osxkeychain .

    如果您的凭据无效,请将其删除:

    git credential-osxkeychain erase
    

    要么:

    printf "protocol=https\nhost=github.com\n" | git credential-osxkeychain erase
    

    因此git不会再次要求您提供钥匙串许可 . 然后再次配置它 .

    请参阅:GitHub上的Updating credentials from the OSX Keychain

    • 您的SSH RSA密钥 .

    为此,您需要将SSH密钥与已添加的密钥进行比较,如果您使用的是正确的标识,请查看 ssh-add -L / ssh-add -l .

    • 您的HTTPS身份验证(如果您使用 https 而不是 ssh 协议) .

    使用 ~/.netrc (Windows上的 %HOME%/_netrc )来提供您的凭据,例如

    machine stash1.mycompany.com
    login myusername 
    password mypassword
    

    了解更多:Syncing with GitHub在SO

  • 60

    我觉得static_rtti提供的答案在某种意义上是hacky . 不知道这是否早期可用,但Git工具现在提供Credential Storage .

    Cache Mode

    $ git config --global credential.helper cache
    

    使用“缓存”模式将凭据保留在内存中一段时间 . 所有密码都没有存储在磁盘上,并且在15分钟后从缓存中清除它们 .

    Store Mode

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

    使用“存储”模式将凭据保存到磁盘上的纯文本文件,它们永不过期 .

    我个人使用 store mode . 删除我的仓库,克隆它然后必须输入我的凭证一次 .

    参考:https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage

  • 682

    如果你可能是由于Github最近在2018-02-22发表了disabling support for deprecated cryptographic algorithms,在这种情况下解决方案就是下载并安装最新版本的完整Git for Windows或只是Git Credential Manager for Windows .

  • 4
    git config credential.helper store
    

    注意:虽然这很方便,但Git会将您的凭据以明文形式存储在项目目录下的本地文件(.git-credentials)中(请参阅下面的“home”目录) . 如果您不喜欢这样,请删除此文件并切换到使用缓存选项 .

    如果您希望Git每次需要连接到远程存储库时都要继续询问您的凭据,您可以运行以下命令:

    git config --unset credential.helper
    

    将密码存储在.git-credentials中%HOME%目录而不是项目目录:使用--global标志

    git config --global credential.helper store
    
  • 2

    使用以下命令增加超时时间,以便您可以重新键入密码一段时间

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

    我把它用于Bitbucket和Github它适用于两者 . 你需要做的只是 3600 是几秒钟,把它增加到你想要的任何程度 . 我把它改为 259200 这大约是30天 . 这样我每隔30天左右重新输入一次密码 .

  • 1

    在Windows for git 1.7.9中,在命令提示符下运行以下命令,在文本编辑器中打开配置文件:

    git config --global --edit
    

    然后在文件中添加以下块(如果不存在)或相应地编辑它

    [credential "https://giturl.com"]
            username = <user id>
            helper = wincred
    

    保存并关闭文件 . 在上述更改后,您只需提供一次凭证 .

  • 1

    第1步:检查您当前的配置

    cat .git/config
    

    你会得到:

    [core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
    [remote "origin"]
        url = https://github.com/path_to_your_git.git
        fetch = +refs/heads/*:refs/remotes/origin/*
    [user]
        name = your_username
        email = your_email
    [branch "master-staging"]
        remote = origin
        merge = refs/heads/master-staging
    

    第2步:删除远程源

    git remote rm origin
    

    步骤3:使用您的用户名和密码添加远程原点

    git remote add origin https://your_git_username:your_git_password@github.com/path_to_your_git.git
    
  • 0

    在URL中输入拼写错误会让git向你询问用户名和密码,愚蠢的git

    在Linux Kali,Git 2.7.0版上测试过

    尝试:

    git clone https://github.com/thisrepodoesntexists/doesntexists.git

  • 7

    使用此:用适当的主机名替换github.com

    git remote set-url origin git@github.com:user/repo.git
    
  • 13

    如果Git每次尝试与GitHub交互时都会提示您输入用户名和密码,那么您可能正在使用存储库的HTTPS克隆URL .

    使用HTTPS远程URL有一些优点:它比SSH更容易设置,并且通常通过严格的防火墙和代理工作 . 但是,每次提取或推送存储库时,它还会提示您输入GitHub凭据 .

    您可以配置Git为您存储密码 . For windows

    git config --global credential.helper wincred
    
  • 6

    当我升级到macOS Sierra时,这发生在我身上 . 看起来ssh代理在升级后被清除了 .

    $ ssh-add -L
    The agent has no identities.
    

    只需运行 ssh-add 找到我现有的身份,输入密码就可以了 .

  • 1

    我想你可能有错误的git repo url .

    打开 .git/config 并找到[remote "origin"]部分 . 确保您使用的是 SSH

    ssh://git@github.com/username/repo.git
    

    而不是 httpsgit

    https://github.com/username/repo.git
    git://github.com/username/repo.git
    

    您现在可以仅使用 SSH Key 而不是 usernamepassword 进行验证 .

    [Edit:] 如果Git抱怨 'origin' has already been added ,打开 .config 文件并在 [remote origin] 之后编辑 url = "..." 部分 url = ssh://github/username/repo.git

  • 3

    正如static_rtti所说,改变

    https://github.com/username/repo.git
    git://github.com/username/repo.git
    

    ssh://git@github.com/username/repo.git
    

    我自己将.git / config文件中的https更改为ssh,但它仍然无法正常工作 . 然后我看到你必须将github.com改为git@github.com . 获取实际正确URL的一个好方法是转到项目页面并单击此按钮

    Change HTTPS to SSH to get the right URL

    然后将此URL添加到配置文件中 .

  • 113

    我同意“codehugger”并使用“orkoden”的说明它对我有用 - 在netbeans 7.3上 - 当你右键单击文件并选择上下文菜单 - 推 - 一个'推送到远程'窗口打开 - 有2个选项在这里

    • origin:https://github.com/myaccount/myproject.git/

    • https://github.com/myaccount/myproject.git/

    正如你所看到的那样,区别在于url中的origin param - 你不想选择这个选项(1)你想检查选项(2),这对我来说很合适 . 我希望这有助于某人 .

    窦乐

  • 5

    Windows上的git指南和使用SSH推送/拉取的Github http://nathanj.github.io/gitguide/tour.html

    • 下载并安装putty

    • 设置环境变量'GIT_SSH' = 'path\to\plink.exe'(在已安装的putty文件夹中) - 非常重要!

    • RESTART WINDOWS EXPLORER for Env变量生效(不仅可以重启命令提示符)

    • 运行puttygen.exe生成新密钥,将PUBLIC密钥复制到Github站点

    • 将这个新的私钥保存在磁盘上安全的地方(首选不是dropbox)

    • 运行putty.exe并将SSH连接到github.co

    • 通过运行"shell:startup"快速进入启动文件夹 . 8通过选美使用Windows启动私钥 . 使用语法"path\to\pageant.exe" "path\to\privatekey"在Startup文件夹中创建快捷方式

    • 我们不需要在我们的回购的.git / config中设置'puttykeyfile'设置

    • 非常重要的是使用了"SSH clone URL"的Github而不是Https

  • 0

    orkoden关于在你的终端中使用带有git的钥匙串的答案是不完整的,并引发了错误 . 这是您需要做的,以保存您在钥匙串中的终端输入的用户名和密码:

    curl http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain -o git-credential-osxkeychain
    sudo mv git-credential-osxkeychain /usr/local/bin
    sudo chmod u+x /usr/local/bin/git-credential-osxkeychain
    

    然后输入

    git config --global credential.helper osxkeychain
    

    如果你已经在curl之前用git config完成了这个部分,那就没问题了,它会工作的

相关问题