Skip to content

Instantly share code, notes, and snippets.

@brianvanderlugt
Last active November 13, 2020 16:27
Show Gist options
  • Save brianvanderlugt/feec88b9d391b4e869ff36d8e8889054 to your computer and use it in GitHub Desktop.
Save brianvanderlugt/feec88b9d391b4e869ff36d8e8889054 to your computer and use it in GitHub Desktop.
GitRebaseBranches
param(
[Parameter(Mandatory)]
[String[]]$Branches
)
$newLineChar = [Environment]::NewLine
Write-Output $newLineChar
[String[]]$commitCountMsgs = @()
[String[]]$scriptOutputLines = @()
for ($i=0;$i -lt ($Branches.Count - 1); $i++){
$baseBranch = $Branches[$i]
$rebaseBranch = $Branches[$i + 1]
$mergeBaseCmd = "git merge-base $baseBranch $rebaseBranch"
$mergeBase = Invoke-Expression -Command $mergeBaseCmd
$branchLastRefCmd = "git rev-parse $rebaseBranch"
$branchLastRef = Invoke-Expression -Command $branchLastRefCmd
$commitCountCmd = "git rev-list ${mergeBase}..${branchLastRef} --count"
$commitCount = Invoke-Expression -Command $commitCountCmd
$commitCountMsgs += "$commitCount commits between $baseBranch & $rebaseBranch."
$scriptOutputLines += "git checkout $rebaseBranch"
$scriptOutputLines += "git rebase --onto $baseBranch ${rebaseBranch}~${commitCount} $rebaseBranch"
}
Write-Output $commitCountMsgs
Write-Output $newLineChar
Write-Output $scriptOutputLines
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment