如何在不输入凭据的情况下从 powershell git clone?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29959502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:57:45  来源:igfitidea点击:

How can I git clone from powershell without typing credentials?

gitpowershellcredentials

提问by monkzen

What I am trying to do is to create one of the steps in CI pipeline that is responsible for git operations. Let say external user provides git URL, username and password. I'd like to have something like that:

我想要做的是在 CI 管道中创建负责 git 操作的步骤之一。假设外部用户提供 git URL、用户名和密码。我想要这样的东西:

function Get-GitClone{
   param([string]$URL,
         [string]$username,
         [string]$password
   )

   # Create temp folder
   cd C:\Temp
   mkdir GitTemp

   # Clone TFD Git repository
   git clone $URL
}

What I'm getting is error running this script, simply because is you run this from powershell it will ask for credentials. I have tried git-credential-winstore.exe, but it still needs user interaction Google doesn't help either or I just can't find anything useful.

我得到的是运行这个脚本的错误,仅仅是因为你是从 powershell 运行它的,它会要求提供凭据。我已经尝试过 git-credential-winstore.exe,但它仍然需要用户交互 Google 也无济于事,或者我找不到任何有用的东西。

Thanks in advance

提前致谢

回答by Rodrigo Nonose

Have you tried $username:$password@$URL?

你试过 $username:$password@$URL 吗?

回答by Vincent De Smet

Are you using ssh or https?

您使用的是 ssh 还是 https?

If you use ssh you could launch a key agent (for example pageant + make git use plink) you'd need to instruct git to use plink for ssh. below is a snippet of a powershell script I use to setup my environment

如果您使用 ssh,您可以启动一个关键代理(例如 pageant + make git use plink),您需要指示 git 将 plink 用于 ssh。下面是我用来设置我的环境的 powershell 脚本的片段

#first add putty to PATH environment variable:
[System.Environment]::SetEnvironmentVariable("PATH",($env:Path + ";C:\Program Files (x86)\PuTTY\ "))

#now where.exe shld find plink too!
[System.Environment]::SetEnvironmentVariable("GIT_SSH",(where.exe plink),"Machine")

You'd need to launch pageant to load the private key, and you would not be able to password protect for the script, for this reason - you could use a deploy-key(with no write access to the repo)

您需要启动选美来加载私钥,并且您将无法为脚本设置密码保护,因此 - 您可以使用 a deploy-key(没有对 repo 的写访问权限)

if you're not using ssh, due to firewall issues - you may need to come up with something else

如果您不使用 ssh,由于防火墙问题 - 您可能需要想出其他办法