Skip to content

Instantly share code, notes, and snippets.

@Seekatar
Created September 5, 2023 22:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Seekatar/513af62e31a1886010289472a97e2e66 to your computer and use it in GitHub Desktop.
Save Seekatar/513af62e31a1886010289472a97e2e66 to your computer and use it in GitHub Desktop.
Script to checkout main branch, deleting current one
<#
.SYNOPSIS
Delete current branch and switch to another one
.DESCRIPTION
This will pull latest from new branch
.PARAMETER NewBranch
Branch to switch to
.PARAMETER Remote
Override the remote to pull from
.EXAMPLE
Switch-Branch
Delete current branch and switch to development branch
.EXAMPLE
Switch-Branch -NewBranch 'main'
Delete current branch and switch to main branch
#>
function Switch-Branch {
[CmdletBinding()]
param(
[string] $NewBranch = 'development',
[string] $Remote= 'origin'
)
$status = Get-GitStatus
if ($status.HasWorking) {
Write-Warning "Working directory not clean"
}
if ((Read-Host "Do you want to delete '$($status.Branch)' branch and switch to '${NewBranch}'? [y/N]") -eq 'y') {
git fetch $Remote "${NewBranch}:$NewBranch"
if ($LASTEXITCODE -eq 0) {
git checkout $newBranch
git branch -D $status.Branch
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment