git branch -r --merged throw an error: fatal: ambiguous argument 'origin/HEAD -> origin/master': unknown revision or path not in the working tree

git branch -r --merged throw an error: fatal: ambiguous argument 'origin/HEAD -> origin/master': unknown revision or path not in the working tree

我写了一个脚本来寻找陈旧的分支。 我检查了未合并和合并的分支。 当我检查合并的分支时,出现错误:

fatal: ambiguous argument 'origin/HEAD -> origin/master': unknown revision or path not in the working tree. Use '--' to separate paths from revisions, like this: 'git [...] -- [...]' You cannot call a method on a null-valued expression. At D:\Repo\Any\FindStaleBranches.ps1:46 char:9 $date = $command.ToString().Substring(0, $command.ToString(). ...

You cannot call a method on a null-valued expression. At D:\Repo\Any\FindStaleBranches.ps1:47 char:9 $commandResult = $command.ToString().split(",", 4)

第46行是“git日志”命令 这是我的脚本:


function GetBranchListInformations()
{
    [CmdletBinding ()]
    Param
    (
        [Parameter(
            Mandatory,
            Position=0)]
            [Object[]] $GitCommandParameters
        )
    
    $branches = & git $GitCommandParameters
    
    if( [string]::IsNullOrWhitespace($branches.ToString()) )
    {
        Write-Information "No branch found" -InformationAction Continue
        return
    }
    
    foreach( $branch in $branches )
    {
        $command = ""
        $branch = $branch.Trim()
        $command = git log -n 1 --format="%cr, %an, %ae, $branch" --no-merges --first-parent $branch | Sort-Object
        $date = $command.ToString().Substring(0, $command.ToString().IndexOf(","))
        $commandResult = $command.ToString().split(",", 4)
        $date = $commandResult[0].Trim()
    
        if( $date -match "year" -or $date -match "years" )
        {
            $commandResult = $command.ToString().split(",", 5)
            [PSCustomObject]@{
                Owner = $commandResult[2].Trim()
                EMail = $commandResult[3].Trim()
                Branch = $branch
                Delay = $date + ", " + $commandResult[1].Trim()
            }
            continue
        }
        if( $date -match "months" )
        {
            $branchCount = $date.ToString().Substring(0, $date.ToString().IndexOf(" "))
            if( $branchCount -gt 3 )
            {
                [PSCustomObject]@{
                    Owner = $commandResult[1].Trim()
                    EMail = $commandResult[2].Trim()
                    Branch = $branch
                    Delay = $date
                }
            }
        }
    }
}
    
Set-Location $PSScriptRoot
    
Write-Information "`nNot merged branches:" -InformationAction Continue
$Params = @(
    "branch",
    "-r",
    "--no-merged
    )
$staleBranchList = GetBranchListInformations $Params | Sort-Object -Property Owner, Delay
foreach( $staleBranch in $staleBranchList )
{
    $owner = $staleBranch.Owner
    $branch = $staleBranch.Branch
    $delay = $staleBranch.Delay
    $mail = $staleBranch.EMail
    Write-Output "$owner, $branch, $delay"
}
    
Write-Information "merged branches:" -InformationAction Continue
$Params = @(
    "branch",
    "-r",
    "--merged"
    )
$staleBranchList = GetBranchListInformations $Params | Sort-Object -Property Owner, Delay
foreach( $staleBranch in $staleBranchList )
{
    $owner = $staleBranch.Owner
    $branch = $staleBranch.Branch
    $delay = $staleBranch.Delay
    $mail = $staleBranch.EMail
    Write-Output "$owner, $branch, $delay"
}

我认为您的问题出在 git branch 上:在某些情况下,它会在分支名称上提供额外的注释(例如:在列出本地分支时,它会在活动分支前加上前缀 "*" ).

运行 git branch -r --merged 在终端中检查显示的内容。


尝试用git for-each-ref代替:

git for-each-ref --merged HEAD --format="%(refname:lstrip=2)" refs/remotes/origin

# if you have several remotes, and want to view all remotes :
# replace 'refs/remotes/origin' with 'refs/remotes'