Tuesday 10 May 2016

Delete Git remote branches using the powershell


Delete Git remote branches using the PowerShell:

Over the period of time GIT branches grow and it is unnecessary to maintain all the history of branches. But to delete all the remote merged branches there is no straight away method.

Here is the powershell script which is very handy and easily understandable to do all the stuff.



#Step 1: Navigate to git folder. This script should has to run the git repo folder
#Step 2: Before running the script make sure you comment the git push origin --delete command. Once all the branch names verified then uncomment and run again.

function gitBranchName {
    $currentBranch = ''
    git branch -r | foreach {
        
        if ($_ -like "*/Fix/*") {
            $currentBranch = $_
            $currentBranch = $currentBranch -replace "origin/", ""
            $currentBranch = $currentBranch -replace " ", ""
            Write-Output($currentBranch)
            git push origin --delete --force $currentBranch
        }
    }
}

gitBranchName


By running the above script in powershell it will delete all the remote branches containes branch name 'Fix'.

Happy coding :)