我希望能夠通過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