我希望能够从与我的 git 存储库不在同一文件夹中的 PowerShell 脚本执行 git 命令。
首先我想检查当前分支,如果不是master,尝试checkout master和pull master。
到目前为止我做了什么:
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 repo 的文件夹的路径。
我得到错误:
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
):
我没有任何错误, pop 窗口打开和关闭的速度非常快,然后我的 powershell 脚本卡住了,就好像它在等待 git-cmd.exe 关闭一样。但是我看不到cmd git的窗口,也无法关闭。
最佳答案
关于git - 使用 PowerShell 获取当前 git 分支的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42903289/