forked from brianary/scripts
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Rename-GitHubLocalBranch.ps1
50 lines (40 loc) · 1.57 KB
/
Rename-GitHubLocalBranch.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<#
.SYNOPSIS
Rename a git repository branch.
.FUNCTIONALITY
Git and GitHub
.LINK
https://docs.github.com/en/github/administering-a-repository/managing-branches-in-your-repository/renaming-a-branch#updating-a-local-clone-after-a-branch-name-changes
.LINK
https://github.com/github/renaming
.LINK
Use-Command.ps1
.EXAMPLE
Rename-GitHubLocalBranch.ps1 main
Rename the current branch to "main".
#>
#Requires -Version 3
[CmdletBinding(ConfirmImpact='High',SupportsShouldProcess=$true)] Param(
# The new branch name.
[Parameter(Position=0,Mandatory=$true)][string] $NewName
)
if(!$PSCmdlet.ShouldContinue('Have you renamed the branch in the GitHub UI?','GitHub Status'))
{
Write-Info.ps1 'Rename the branch via the GitHub UI before running this script.'
if((Get-Command gh -CommandType Application -ErrorAction Ignore)) {gh browse}
Start-Process 'https://docs.github.com/en/github/administering-a-repository/managing-branches-in-your-repository/renaming-a-branch#renaming-a-branch'
return
}
$oldName = git rev-parse --abbrev-ref HEAD
if(!$PSCmdlet.ShouldProcess("$oldName branch","rename to $NewName")) {return}
Use-Command.ps1 git "$env:ProgramFiles\Git\cmd\git.exe" -choco git
Write-Verbose "git branch -m $oldName $NewName"
git branch -m $oldName $NewName
Write-Verbose "git fetch origin"
git fetch origin
Write-Verbose "git branch -u origin/$NewName $NewName"
git branch -u "origin/$NewName" $NewName
Write-Verbose 'git remote set-head origin -a'
git remote set-head origin -a
Write-Verbose 'git remote prune origin'
git remote prune origin