我希望能够通过PowerShell脚本执行git命令,该脚本与我的git repo不在同一文件夹中。使用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
从文档here答案和this question。
$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
编辑:(使用Join-Path
)@ 4c74356b41的命题后新的问题: 我没有一个弹出打开任何错误和关闭非常快,然后我的powershell脚本卡住了,好像它正在等待git-cmd.exe关闭。但是我看不到cmd git的窗口,无法关闭它。
您是否知道辣妹的Git的? https://github.com/dahlbyk/posh-git –
posh-git不会以任何方式帮助形状或形式 – 4c74356b41
阅读你的代码示例,你已经在你的回购。这意味着,像@Mark所说的那样,posh-git会有所帮助。 (例如'Get-GitDirectory') – Clijsters