Git命令中的Windows命令行变量



为了使git remote add origin %repoWithToken%解析为预期的有效URL,需要在下面的windows命令行命令中更改哪些特定语法?

失败的命令:

以下命令在windows-latestGitHub运行程序上的pwshshell中运行。

set repoWithToken="https://"$GIT_PAT"@github.com/accountName/repoName.git"
git init
git remote add origin %repoWithToken%

错误消息:

当运行上述代码时,GitHub windows最新运行程序会给出以下错误消息:

fatal: '%repoWithToken%' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Error: Process completed with exit code 1.

答案可以只是简单的windows命令行命令。不需要任何花哨的PowerShell。

此外,$GIT_PAT是正确的GitHub语法,尽管set repoWithToken="https://"$GIT_PAT"@github.com/accountName/repoName.git"中的连接可能需要在上面的代码中以不同的方式进行。这是从Bash翻译过来的。

基于Olaf的有益评论:

  • set <varName>=<valuecmd.exe中用于分配变量;而在PowerShell中,setSet-Variable的内置别名,后者具有非常不同的语法

    • 更重要的是,很少需要使用它,因为变量被分配为$<varName> = <value>

    • PowerShell在设置获取变量值$<varName>时使用相同的语法;因此,在用$repoWithToken = <value>分配给名为repoWithToken的变量后,您稍后也用$repoWithToken引用其(相比之下,%repoWithToken%再次是cmd.exe语法(。

  • 假设GIT_PAT环境变量的名称,则必须在PowerShell变量中将其称为$env:GIT_PAT(带有显式名称描述:${env:GIT_PAT}(

  • 与Bash(以及通常与POSIX兼容的shell(不同,PowerShell只允许您在第一个令牌未被引用的情况下,由带引号和未被引号的令牌混合组成单个字符串。

    • 因此,类似"https://"$GIT_PAT"@github.com/accountName/repoName.git"的东西不能作为单个字符串参数工作,因为它的第一个标记是引用,导致PowerShell在这种情况下将其分解为两个参数。

    • 由于这里需要字符串插值来用其值替换${env:GIT_PAT},因此只需将整个值包含在"..."中,即使其成为可扩展的字符串

因此:

$repoWithToken = "https://${env:GIT_PAT}@github.com/accountName/repoName.git"
git init
git remote add origin $repoWithToken

最新更新