首页 文章

在GitHub上使用https://时有没有办法跳过密码输入?

提问于
浏览
1679

我最近切换到将我的存储库同步到GitHub上的https://(由于防火墙问题),并且每次都要求输入密码 . 曾经是我有一个SSH证书,这就足够了 . 在我的情况下有没有办法绕过密码(使用http / https)?

23 回答

  • 11

    使用Git 1.7.9及更高版本

    自Git 1.7.9(2012年1月下旬发布)以来,Git中有一个简洁的机制,可以避免一直为HTTP / HTTPS输入密码,称为credential helpers . (感谢dazonic在下面的评论中指出了这个新功能 . )

    使用Git 1.7.9或更高版本,您可以使用以下凭证助手之一:

    git config --global credential.helper cache
    

    ...告诉Git将密码缓存在内存中(默认情况下)为15分钟 . 您可以使用以下设置更长的超时:

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

    (该示例在GitHub help page for Linux中有建议 . )如果需要,您还可以永久存储您的凭据,请参阅下面的其他答案 .

    GitHub的帮助also suggests,如果您使用的是Mac OS X并使用Homebrew来安装Git,则可以使用本机Mac OS X密钥库:

    git config --global credential.helper osxkeychain
    

    对于Windows,有一个名为Git Credential Manager for Windowswincred in msysgit的帮助程序 .

    git config --global credential.helper wincred # obsolete
    

    Git for Windows 2.7.3+(2016年3月):

    git config --global credential.helper manager
    

    对于Linux,您可以use gnome-keyring(或其他密钥环实现,如KWallet) .

    使用1.7.9之前的Git版本

    对于1.7.9之前的Git版本,这个更安全的选项不可用,您需要更改 origin 远程用来以这种方式包含密码的URL:

    https://you:password@github.com/you/example.git
    

    ...换句话说,在用户名之后和 @ 之前使用 :password .

    您可以使用以下命令为 origin 遥控器设置新的URL:

    git config remote.origin.url https://you:password@github.com/you/example.git
    

    确保你使用 https ,你应该知道,如果这样做,你的GitHub密码将以明文形式存储在 .git 目录中,这显然是不可取的 .

    使用任何Git版本(好吧,因为版本0.99)

    另一种方法是将您的用户名和密码放在 ~/.netrc 文件中,但是,与将密码保存在远程URL中一样,这意味着您的密码将以纯文本形式存储在磁盘上,因此安全性较低,不建议使用 . 但是,如果要采用此方法,请将以下行添加到 ~/.netrc

    machine <hostname> login <username> password <password>
    

    ...用服务器的主机名替换 <hostname> ,用你的用户名和密码替换 <username><password> . 还记得在该文件上设置限制性文件系统权限:

    chmod 600 ~/.netrc
    

    请注意,在Windows上,此文件应称为 _netrc ,您可能需要定义%HOME%环境变量 - 有关更多详细信息,请参阅:

  • 5

    对于Windows,您可以使用Git Credential Manager(GCM)插件 . 它目前由Microsoft维护 . 好处是它将密码保存在Windows Credential Store中,而不是纯文本 .

    项目的releases page上有一个安装程序 . 这也将安装Git for Windows的官方版本,内置凭证管理器 . 它允许two-factor authentication用于GitHub(和其他服务器) . 并具有用于初始登录的图形界面 .

    对于Cygwin用户(或已经使用官方Git for Windows的用户),您可能更喜欢手动安装 . 从releases page下载zip包 . 解压缩包,然后运行 install.cmd 文件 . 这将安装到 ~/bin 文件夹中 . (确保 ~/bin 目录位于PATH中 . )然后使用此命令对其进行配置:

    git config --global credential.helper manager
    

    然后,当对任何服务器进行身份验证时,Git将运行 git-credential-manager.exe .

  • 666

    最好使用凭据进行安全性,但您可以使用缓存将其保留一段时间:

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

    您的凭据将保存3600秒 .

  • 8

    我从gitcredentials(7) Manual Page得到了答案 . 就我而言,我的Windows安装中没有凭证缓存;我使用凭证存储 .

    使用凭证存储后,用户名/密码存储在[用户文件夹] / .git-credentials文件中 . 要删除用户名/密码,只需删除文件的内容即可 .

  • 2

    OAuth

    您可以创建自己的personal API tokenOAuth)并以与使用普通凭据相同的方式使用它(位于: /settings/tokens ) . 例如:

    git remote add fork https://4UTHT0KEN@github.com/foo/bar
    git push fork
    

    .netrc

    另一种方法是在 ~/.netrc (Windows上的 _netrc )中配置您的用户/密码,例如

    machine github.com
    login USERNAME
    password PASSWORD
    

    对于HTTPS:

    machine github.com
    login USERNAME
    password PASSWORD
    protocol https
    

    凭证助手

    对于cache your GitHub password in Git使用HTTPS时,您可以使用凭证助手告诉Git每次与GitHub交谈时都记住您的GitHub用户名和密码 .

    • Mac: git config --global credential.helper osxkeychain (需要 osxkeychain helper ),

    • Windows: git config --global credential.helper wincred

    • Linux和其他: git config --global credential.helper cache


    有关:

  • 16

    克隆 repo 后,您可以编辑 repo/.git/config 并添加如下配置:

    [user]
        name = you_name
        password = you_password
    [credential]
        helper = store
    

    那么你不会再被要求 usernamepassword .

  • 35

    Use a credential store.

    对于 OS XLinux 上的Git 2.11,use Git's built in credential store

    git config --global credential.helper libsecret
    

    对于 Windows 上的msysgit 1.7.9:

    git config --global credential.helper wincred
    

    对于OS X上的Git 1.7.9使用:

    git config --global credential.helper osxkeychain
    
  • 33

    在GNU / Linux设置上,〜/ .netrc也能很好地工作:

    $ cat ~/.netrc
    machine github.com login lot105 password howsyafather
    

    它可能取决于Git用于HTTPS传输的网络库 .

  • 87

    对我来说,我不需要首先下载帮助程序!我在Atlassian's Permanently authenticating with Git repositories找到了credential.helper下载 .

    引用:

    如果要在OS X上使用带有凭据缓存的Git,请执行以下步骤:

    下载二进制git-credential-osxkeychain .

    运行以下命令以确保二进制文件是可执行的:

    chmod a+x git-credential-osxkeychain
    

    把它放在 /usr/local/bin 目录中 .

    运行以下命令:

    git config --global credential.helper osxkeychain
    
  • 2202

    通常你有一个像这样的远程URL,

    git remote -v
    
    origin    https://gitlab.com/username/Repo.git (fetch)
    origin    https://gitlab.com/username/Repo.git (push)
    

    如果您想在使用 git push 时跳过用户名和密码,请尝试以下操作:

    git remote set-url origin https://username:password@gitlab.com/username/Repo.git
    

    我刚刚向原点添加了相同的URL(包括用户详细信息,包括密码) .

    NOTE: 如果用户名是电子邮件ID,则无效 .

    git remote -v
    
    origin    https://username:password@gitlab.com/username/Repo.git (fetch)
    origin    https://username:password@gitlab.com/username/Repo.git (push)
    
  • 31

    您可以使用凭证助手 .

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

    其中 x 是秒数 .

  • 19

    有一种简单,老式的方法可以在HTTPS URL中存储用户凭据:

    https://user:password@github.com/...
    

    您可以使用 git remote set-url <remote-repo> <URL> 更改网址

    这种方法的明显缺点是您必须以纯文本格式存储密码 . 您仍然可以输入用户名( https://user@github.com/... ),这至少可以节省一半的麻烦 .

    您可能更喜欢切换到SSH或使用GitHub客户端软件 .

  • 18

    您可以阻止它使用GitHub API的作曲家文档mentions,以便它像 git clone 一样:

    如果在GitHub存储库中将no-api键设置为true,它将像使用任何其他Git存储库而不是使用GitHub API一样克隆存储库 . 但与直接使用git驱动程序不同,composer仍会尝试使用GitHub的zip文件 .

    所以该部分看起来像这样:

    "repositories": [
        {
            "type": "vcs",
            "no-api": true,
            "url": "https://github.com/your/repo"
        }
    ],
    

    请记住,API存在是有原因的 . 因此,对于github.com上增加的负载,这应该是最后的手段 .

  • 13

    如果你像我一样使用_323452,事情会有所不同 . 因为我没有在这里贴一个,所以也许我以后可以找到它 .

    如果您使用双因素身份验证,则指定用户名/密码甚至不会工作 - 你被拒绝访问 . 但是您可以使用应用程序访问令牌并使用Git的凭据帮助程序为您缓存该令牌 . 以下是相关链接:

    我不记得我在哪里看到这个,但是当你被问到你的用户名时 - 那就是你坚持使用应用程序访问令牌的地方 . 然后将密码留空 . 它适用于我的Mac .

  • 8

    你可以使用

    git config credential.helper store
    

    当您下次使用pull或push输入密码时,它将以.git-credentials格式存储为纯文本(有点不安全,但只需将其放入受保护的文件夹)

    就是这样,如本页所述:

    https://git-scm.com/docs/git-credential-store

  • 7

    这对我有用,我正在使用Windows 10

    git config --global credential.helper wincred
    
  • 5

    只需将登录凭据作为URL的一部分包含在内:

    git remote rm origin 
    git remote add origin https://username:mypassword@github.com/path/to/repo.git
    
  • 5

    您还可以让Git使用以下内容永久存储您的凭据:

    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
    
  • 4

    我知道这不是一个安全的解决方案,但有时你只需要一个简单的解决方案 - 无需安装任何其他东西 . 由于helper = store对我不起作用,我创建了一个虚拟助手:

    创建一个脚本并将其放在用户bin文件夹中,此处名为 credfake ,此脚本将提供您的用户名和密码:

    #!/bin/bash
    while read line
    do
      echo "$line"
    done < "/dev/stdin"
    echo username=mahuser
    echo password=MahSecret12345
    

    使其可执行:

    chmod u+x /home/mahuser/bin/credfake
    

    然后在git中配置它:

    git config --global credential.helper /home/mahuser/bin/credfake
    

    (或仅使用--global用于一个回购)

    并且 - voilá - git将使用此用户密码 .

  • 3

    如果您不想像Mark所说的那样以明文形式存储密码,那么您可以使用不同的GitHub URL进行提取,而不是使用推送 . 在配置文件中,在 [remote "origin"] 下:

    url = git://github.com/you/projectName.git
    pushurl = git@github.com:you/projectName.git
    

    当你推送时它仍然会要求输入密码,但是至少对于开源项目来说,它不会在你获取时提出 .

  • 2

    应使用身份验证令牌而不是帐户密码 . 转到GitHub设置/应用程序,然后创建个人访问令牌 . 令牌的使用方式与使用密码的方式相同 .

    该令牌旨在允许用户不使用帐户密码进行项目工作 . 仅在使用时使用密码做管理工作,比如创建新令牌或撤销旧令牌 .


    可以使用项目特定部署密钥来授予对单个项目存储库的访问权限,而不是授予用户对GitHub帐户的完整访问权限的令牌或密码 . 当您仍然可以使用普通凭据访问其他Git帐户或项目时,可以将Git项目配置为在以下步骤中使用此不同的密钥:

    • 编写一个SSH配置文件,其中包含部署密钥的 HostIdentityFile ,可能是 UserKnownHostsFile ,也许是 User (虽然我认为您不需要它) .

    • 写一个SSH包装器shell脚本,实际上是 ssh -F /path/to/your/config $*

    • 在正常的Git命令前面添加 GIT_SSH=/path/to/your/wrapper . 这里 git remote (原点)必须使用 git@github.com:user/project.git 格式 .

  • 1

    您还可以编辑 bashrc 文件并在其中添加脚本 .

    这将在您启动Git时询问您的密码,然后在您注销之前记住它 .

    SSH_ENV=$HOME/.ssh/environment
      
    # Start the ssh-agent
    function start_agent {
        echo "Initializing new SSH agent..."
    
        # Spawn ssh-agent
        /usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
        echo succeeded
        chmod 600 "${SSH_ENV}"
        . "${SSH_ENV}" > /dev/null
        /usr/bin/ssh-add
    }
      
    if [ -f "${SSH_ENV}" ]; then
         . "${SSH_ENV}" > /dev/null
       ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
          start_agent;
      }
    else
        start_agent;
    fi
    
  • 1

    TLDR;在Git 1.8.3中使用加密的netrc文件 .

    保存Git存储库的密码在Windows上使用 ~/.netrc (Unix)或 %HOME%/_netrc (注意 _ )可以使用HTTPS URL .

    But :该文件将以纯文本格式存储您的密码 .

    Solution :使用GPG (GNU Privacy Guard)加密该文件,并在每次需要密码时使Git解密(对于 push / pull / fetch / clone 操作) .


    注意:使用Git 2.18(2018年第二季度),您现在可以自定义用于解密加密的 .netrc 文件的GPG .

    Luis Marsano ()](https://github.com/),[commit f07eeed](https://github.com/git/git/commit/f07eeed123b8880b1723b1ea9d6d6f41cfb34532)(2018年5月12日)[Luis Marsano () .
    (由Junio C Hamano合并 - gitster - 在提交017b7c5,2018年5月30日)

    git-credential-netrc:接受gpg选项git-credential-netrc被硬编码为'gpg'解密,无论gpg.program选项如何 . 这是像Debian这样的分布上的一个问题,它将现代GnuPG称为其他东西,比如'gpg2'


    Windows的分步说明

    使用Windows:

    (Git在其发行版中有 gpg.exe ,但使用完整的GPG安装包含 gpg-agent.exe ,它将记住与您的GPG密钥关联的密码 . )

    set PATH=%PATH%:C:\path\to\gpg
    copy C:\path\to\gpg\gpg2.exe C:\path\to\gpg\gpg.exe
    

    (注意' copy ' command: Git will need a Bash script to execute the command ' gpg ' . 由于 gpg4win-vanilla-2 附带 gpg2.exe ,您需要复制它 . )

    • 创建或导入GPG密钥,并信任它:
    gpgp --import aKey
    # or
    gpg --gen-key
    

    (确保将密码加到该密钥上 . )

    cd c:\a\fodler\in\your\path
    curl -o c:\prgs\bin\git-credential-netrc https://raw.githubusercontent.com/git/git/master/contrib/credential/netrc/git-credential-netrc
    

    (是的,这是一个Bash脚本,但它可以在Windows上运行,因为它将由Git调用 . )

    • 以明文形式创建_netrc文件
    machine a_server.corp.com
    login a_login
    password a_password
    protocol https
    
    machine a_server2.corp.com
    login a_login2
    password a_password2
    protocol https
    

    (Don 't forget the ' protocol ' part: ' http ' or ' https '取决于您将使用的URL . )

    • 加密该文件:
    gpg -e -r a_recipient _netrc
    

    (您现在可以删除 _netrc 文件,仅保留 _netrc.gpg 加密文件 . )

    • 使用该加密文件:
    git config --local credential.helper "netrc -f C:/path/to/_netrc.gpg -v"
    

    (注意' / ': C:\path\to... 根本不起作用 . )(你可以先使用 -v -d 看看发生了什么 . )

    从现在开始,任何使用需要身份验证的HTTP(S)URL的Git命令都会解密该 _netrc.gpg 文件,并使用与您联系的服务器关联的登录名/密码 . 第一次,GPG会要求您提供GPG密钥的密码,以解密该文件 . 其他时候,第一次GPG呼叫的gpg-agent launched automatically 将为您提供该密码 .

    这样,您可以在一个文件中记住 several URL /登录/密码,并将其存储在磁盘上加密 .
    我发现它比"cache"帮助器更方便“,你需要记住并为每个远程服务键入(每个会话一次)一个不同的密码,以便将所述密码缓存在内存中 .

相关问题