Created
September 5, 2023 22:36
-
-
Save Seekatar/513af62e31a1886010289472a97e2e66 to your computer and use it in GitHub Desktop.
Script to checkout main branch, deleting current one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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