我希望能够从不在git仓库相同文件夹的PowerShell脚本中执行git命令。
首先,我想要检查当前分支,如果它不是主分支,则尝试检出主分支并拉取主分支。
到目前为止我做了什么:
function Get-ScriptDirectory {
Split-Path $script:MyInvocation.MyCommand.Path
}
$currentPath = Get-ScriptDirectory
[string]$Path_GIT = "C:\Program Files\Git\git-cmd.exe"
$gitRepo = $currentPath + "\..\"
$nameCurrentBranch = & $Path_GIT git -C "$gitRepo" rev-parse --abbrev-ref HEAD
根据此处的文档和这个问题的答案,$gitRepo
包含包含git仓库的文件夹的路径。
我收到以下错误:
git-cmd.exe : fatal: Cannot change to 'C:\Users\MyName\Documents\Projects\MyProject\ Batchs\.." rev-parse --abbrev-ref HEAD': Invalid argument At C:\Users\MyName\Documents\Projects\MyProject\Batchs\Publish.ps1:64 char:22 + ... entBranch = & $Path_GIT git -C "$gitRepo" rev-parse --abbrev-ref HEAD ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (fatal: Cannot c...nvalid argument:String) [], RemoteException + FullyQualifiedErrorId : NativeCommandError
编辑:@4c74356b41建议使用Join-Path
后出现了新问题:弹出窗口迅速打开并关闭,然后我的PowerShell脚本就卡住了,就好像它正在等待git-cmd.exe关闭一样。但是我看不到cmd git的窗口,也无法关闭它。
Get-GitDirectory
) - Clijsters