【问题标题】:How to enter command with password for git pull?如何为 git pull 输入带有密码的命令?
【发布时间】:2012-07-15 09:47:20
【问题描述】:

我想在一行中执行此命令:

git pull && [my passphrase]

怎么做?

【问题讨论】:

  • 您可以按照以下步骤将密码改为空:help.github.com/articles/working-with-ssh-key-passphrases
  • 如果您运行 ssh 代理,您可以回避输入密码的需要。第一次执行 git pull 时,您以交互方式执行,ssh 代理会记住您的私钥,您可以在没有提示的情况下运行 git pull。

标签: git bash command


【解决方案1】:

这不是您要求的,而是 http(s):

  • 您可以将密码放在 .netrc 文件中(Windows 上为 _netrc)。从那里它将被自动拾取。它会以 600 个权限进入您的主文件夹。
  • 您也可以使用 https://user:pass@domain/repo 克隆存储库,但不建议这样做,因为它会在很多地方显示您的用户/通行证...
  • 一个新选项是使用credential helper。请注意,凭据将使用标准凭据帮助程序以明文形式存储在本地配置中。带有 wincred 的 credential-helper 也可以在 Windows 上使用。

凭证助手的使用示例

  • git config credential.helper store - 无限期地存储凭据。
  • git config credential.helper 'cache --timeout=3600'- 存放 60 分钟

对于基于 ssh 的访问,您可以使用 ssh 代理,该代理将在需要时提供 ssh 密钥。这需要在您的计算机上生成密钥,将公钥存储在远程服务器上并将私钥添加到相关的密钥库。

【讨论】:

  • 你能详细说明一下SSH部分吗?我正在配置一个 EC2 实例,我希望它在不要求我的密码和 AWS 自动缩放的情况下拉取。这个公共/私人方案看起来是一个很好的解决方案。
  • @PedroDusso 听起来像 this thread 是你所追求的
  • 我结束了使用部署密钥机制,这似乎是一个很好的解决方案。谢谢!
  • 我在下面发布了一个答案,但我最终使用了访问令牌,因为这些方法似乎都不适合我的用例。
【解决方案2】:

我找到了一种在命令行上为 https 连接提供凭据的方法。您只需要指定 git pull 的完整 URL 并在其中包含凭据:

git pull https://username:password@mygithost.com/my/repository

您之前不需要使用凭据克隆存储库,这意味着您的凭据不会以.git/config 结尾。 (但请确保您的 shell 不会背叛您,并将命令行存储在历史文件中。)

【讨论】:

  • 密码不是必需的,这样写密码是不安全的
  • 这不是我在 2012 年已经在接受的答案中列出的内容之一吗?
  • @eis:不完全是。关键是您不需要使用包含凭据的 URL clone 存储库,但仍然可以从具有凭据的 URL pull。效果是凭据不会出​​现在 .git/config 文件中。
  • 如果您省略:password 部分,您将在按回车后提示您输入密码。这样,您的密码将不会保存在 bash 历史记录中。
  • 是的 @PramodGarg 使用 - git pull username:password@mygithost.com/my/repository.git Branch
【解决方案3】:

没有直接回答这个问题,但是我在寻找一种方法时发现了这个问题,基本上,每次拉远程服务器时都不要重新输入密码。。 p>

好吧,git 允许您在有限的时间内缓存您的凭据。它可以在git config 中自定义,这个页面解释得很好:

https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux

在终端中,运行:

$ git config --global credential.helper cache
# Set git to use the credential memory cache

要自定义缓存超时,你可以这样做:

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

然后,您的凭据将在请求的时间内存储在内存中。

【讨论】:

    【解决方案4】:

    如果我们在密码中没有@,则下面的 cmd 将起作用: git pull https://username:pass@word@mygithost.com/my/repository 如果密码中有@,则将其替换为 %40,如下所示: git pull https://username:pass%40word@mygithost.com/my/repository

    【讨论】:

    • 谢谢,我正在寻找密码相同模式的解决方案
    【解决方案5】:

    使用凭证助手命令行option

    git -c credential.helper='!f() { echo "password=mysecretpassword"; }; f' fetch origin
    

    【讨论】:

      【解决方案6】:

      我刚刚经历了这个,所以提供了我的答案,因为我最终使用了Gitlab access tokens,尽管有些人可能需要Github access tokens

      我更喜欢使用 SSH,但有一个 gitlab 错误阻止了它。将我的密码放在 .netrc 或 URL 中并不理想。凭据管理器需要在服务器重新启动时重新启动,这并不理想。

      因此我选择了一个可以像这样使用的访问令牌:git clone https://<username>:<accessToken>@gitlab.com/ownerName/projectName.git

      访问令牌与 url 一起存储,因此这并不安全,但它比使用 username:password 更安全,因为访问令牌可以仅限于某些操作并且可以轻松撤销。

      一旦它被克隆下来(或手动更新 URL),您的所有 git 请求都将使用访问令牌,因为它存储在 URL 中。

      如果您希望更新已克隆的存储库,这些命令可能会有所帮助:

      git remote show origin
      
      git remote remove origin
      
      git remote add origin https://<username>:<accessToken>@gitlab.com/ownerName/projectName.git
      

      编辑:一定要检查下面的 cmets。我添加了 2 个重要说明。

      【讨论】:

      • 更新:Gitlab 同时拥有用户访问和项目访问令牌。如果您使用像 Okta 这样的 SSO 提供商,您将需要使用项目访问令牌。用户令牌将需要通过 SSO 不断重新验证,使其无法用于自动化流程。
      • 更新2:一班更新遥控器git remote set-url origin https://&lt;username&gt;:&lt;accessToken&gt;@gitlab.com/ownerName/projectName.git
      【解决方案7】:

      你可以只使用用户名,不需要密码,因为密码会在 git bash 中提示。

      git pull https://username@github.com/username/myrepo
      

      【讨论】:

      【解决方案8】:

      请注意,git 凭证助手“存储”的方式将 store the unencrypted passwords 随着 Git 2.5+(2014 年第二季度)的变化而变化。
      commit 17c7f4dJunio C Hamano (gitster)

      credential-xdg

      调整凭据帮助程序的示例“store”后端,以在指定时遵守 XDG 配置文件位置。

      医生现在说:

      如果没有指定:

      • 将从~/.git-credentials$XDG_CONFIG_HOME/git/credentials 中搜索凭据,并且
      • 凭证将写入~/.git-credentials(如果存在)或$XDG_CONFIG_HOME/git/credentials(如果存在而前者不存在)。

      【讨论】:

        【解决方案9】:

        在 Google 和 stackoverflow 搜索了一段时间后,我没有找到问题的答案,所以我想在这里分享我的解决方案。

        git config --global credential.helper "/bin/bash /git_creds.sh"
        echo '#!/bin/bash' > /git_creds.sh
        echo "sleep 1" >> /git_creds.sh
        echo "echo username=$SERVICE_USER" >> /git_creds.sh
        echo "echo password=$SERVICE_PASS" >> /git_creds.sh
        
        # to test it
        git clone https://my-scm-provider.com/project.git
        

        我也是为 Windows 做的。 Full answer here

        【讨论】:

          【解决方案10】:

          如果您希望在 Gitlab 上的 CI/CD 脚本中执行此操作 (gitlab-ci.yml)。你可以使用

          git pull $CI_REPOSITORY_URL
          

          这将转化为:

          git pull https://gitlab-ci-token:[MASKED]@gitlab.com/gitlab-examples/ci-debug-trace.gi
          

          而且我很确定它使用的令牌是一个临时/每个作业令牌 - 所以这种方法的安全漏洞大大减少了。

          【讨论】:

            猜你喜欢
            • 2018-05-21
            • 2014-02-06
            • 1970-01-01
            • 2019-07-18
            • 2022-01-25
            • 2022-01-08
            • 2019-11-20
            • 2013-05-20
            • 1970-01-01
            相关资源
            最近更新 更多