简体   繁体   English

git 命令中的 Windows 命令行变量

[英]Windows command line variable in git command

What specific syntax needs to be changed in the windows command line commands below in order for git remote add origin %repoWithToken% to resolve to the intended valid URL?为了使git remote add origin %repoWithToken%解析为预期的有效 ZE6B391A8D2C4D45902A23A8B6585703D,需要在下面的 windows 命令行命令中更改哪些特定语法?

COMMANDS THAT ARE FAILING:失败的命令:

The following commands are run in a pwsh shell on a windows-latest GitHub runner.以下命令在windows-latest GitHub 运行器上的pwsh shell 中运行。

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

ERROR MESSAGE:错误信息:

The following error message is given by the GitHub windows-latest runner when the above code is run:运行上述代码时,GitHub windows-latest runner 给出以下错误消息:

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.

The answer can just be simple windows command line command.答案可以只是简单的 windows 命令行命令。 No need for any fancy powershell.不需要任何花哨的 powershell。

Also, $GIT_PAT is correct GitHub syntax, though it is possible that the concatenation in set repoWithToken="https://"$GIT_PAT"@github.com/accountName/repoName.git" might need to be done differently in the code above.此外, $GIT_PAT是正确的 GitHub 语法,尽管set repoWithToken="https://"$GIT_PAT"@github.com/accountName/repoName.git"中的连接可能需要在上面的代码中以不同方式完成. This is in the process of being translated from bash.这是从 bash 翻译的过程中。

Building on Olaf 's helpful comments:基于Olaf的有用评论:

  • set <varName>=<value is used in cmd.exe for assigning variables; set <varName>=<valuecmd.exe中用于分配变量; while in PowerShell set is a built-in alias for Set-Variable , the latter has very different syntax .而在 PowerShell 中, setSet-Variable的内置别名,后者有非常不同的语法

    • More importantly, its use is rarely needed, because variables are assigned as $<varName> = <value>更重要的是,它很少需要使用,因为变量被分配为$<varName> = <value>

    • PowerShell uses the same syntax on setting and getting variable values, $<varName> ; PowerShell 在设置获取变量值时使用相同的语法$<varName> thus, after having assigned to a variable named repoWithToken with $repoWithToken = <value> , you also refer to its value later with $repoWithToken (by contrast, %repoWithToken% is again cmd.exe syntax).因此,在使用$repoWithToken = <value>分配给名为repoWithToken的变量之后,您还可以稍后使用$repoWithToken引用它的(相比之下, %repoWithToken%再次是cmd.exe语法)。

  • Assuming that GIT_PAT is the name of an environment variable, you must refer to it as $env:GIT_PAT in PowerShell variable (with explicit name delineation: ${env:GIT_PAT} )假设GIT_PAT环境变量的名称,您必须在 PowerShell 变量中将其称为$env:GIT_PAT (具有明确的名称描述: ${env:GIT_PAT}

  • Unlike Bash (and POSIX-compatible shells in general), PowerShell only allows you to compose a single string from a mix of quoted and unquoted tokens if the first token is unquoted.与 Bash(以及一般与 POSIX 兼容的 shell)不同,如果第一个标记未加引号,PowerShell 仅允许您从带引号和未加引号的标记的混合中组合单个字符串。

    • Something like "https://"$GIT_PAT"@github.com/accountName/repoName.git" therefore doesn't work as a single string argument, because its first token is quoted , causing PowerShell to break it into two arguments in this case.因此,像"https://"$GIT_PAT"@github.com/accountName/repoName.git"这样的东西不能用作单个字符串参数,因为它的第一个标记被引用,导致 PowerShell 将它分成两个arguments案子。

    • Since string interpolation is needed here in order to replace ${env:GIT_PAT} with its value, simply enclose the entire value in "..." , ie make it an expandable string由于此处需要字符串插值以将${env:GIT_PAT}替换为其值,因此只需将整个值括在"..."中,即使其成为可扩展字符串

Therefore:所以:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM