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

Issue

I write a script that are looking for stale branches.
I checked the not merged and merged branches.
When i check the merged branches i get an error:

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)

Line 46 is the "git log" command
here is my script:


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"
}

Solution

I think your issue is with git branch : in some cases, it will give extra annotations on a branch’s name (e.g : when listing local branches, it prefixes the active branch with a "*").

Run git branch -r --merged in a terminal to check what is displayed.


Try to replace it with 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'

[edit] I since found out git branch also accepts a --format option, which makes it produce the same output as git for-each-ref — with a filter on branches :

git branch --merged HEAD -r --format="%(refname:short)"
git branch --merged HEAD -r --format="%(refname:short)" --list "origin/*"

Answered By – LeGEC

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published