PowerShell/docs/switch-branch.md

129 lines
4.2 KiB
Markdown
Raw Normal View History

2024-01-25 13:37:12 +01:00
Script: *switch-branch.ps1*
========================
2021-11-08 21:36:42 +01:00
2022-02-10 09:01:07 +01:00
This PowerShell script switches to another branch in a Git repository (including submodules).
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Parameters
----------
2021-11-08 21:36:42 +01:00
```powershell
2023-10-19 08:12:00 +02:00
PS> ./switch-branch.ps1 [[-branchName] <String>] [[-repoDir] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2023-10-19 08:12:00 +02:00
-branchName <String>
2021-11-08 21:36:42 +01:00
Specifies the branch name
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
2023-10-19 08:12:00 +02:00
-repoDir <String>
2022-12-04 10:40:18 +01:00
Specifies the path to the local Git repository
2021-11-08 21:36:42 +01:00
Required? false
Position? 2
Default value "$PWD"
Accept pipeline input? false
Accept wildcard characters? false
[<CommonParameters>]
This script supports the common parameters: Verbose, Debug, ErrorAction, ErrorVariable, WarningAction,
WarningVariable, OutBuffer, PipelineVariable, and OutVariable.
```
2023-07-29 10:04:38 +02:00
Example
-------
2021-11-08 21:36:42 +01:00
```powershell
PS> ./switch-branch main C:\MyRepo
2023-09-13 09:49:05 +02:00
⏳ (1/6) Searching for Git executable... git version 2.42.0.windows.1
⏳ (2/6) Checking local repository...
⏳ (3/6) Fetching updates...
⏳ (4/6) Switching to branch 'main'...
⏳ (5/6) Pulling updates...
⏳ (6/6) Updating submodules...
2023-10-19 08:12:00 +02:00
✔️ Switched repo 📂MyRepo to branch 'main' in 22 sec
2021-11-08 21:36:42 +01:00
```
2023-07-29 10:04:38 +02:00
Notes
-----
2022-11-17 19:46:02 +01:00
Author: Markus Fleschutz | License: CC0
2021-11-08 21:36:42 +01:00
2023-07-29 10:04:38 +02:00
Related Links
-------------
2021-11-08 21:36:42 +01:00
https://github.com/fleschutz/PowerShell
2023-07-29 10:04:38 +02:00
Script Content
--------------
2022-11-17 20:05:34 +01:00
```powershell
2022-11-17 20:02:26 +01:00
<#
.SYNOPSIS
Switches the Git branch
.DESCRIPTION
This PowerShell script switches to another branch in a Git repository (including submodules).
2023-10-19 08:12:00 +02:00
.PARAMETER branchName
2022-11-17 20:02:26 +01:00
Specifies the branch name
2023-10-19 08:12:00 +02:00
.PARAMETER repoDir
2022-12-04 10:40:18 +01:00
Specifies the path to the local Git repository
2022-11-17 20:02:26 +01:00
.EXAMPLE
PS> ./switch-branch main C:\MyRepo
2023-09-13 09:49:05 +02:00
⏳ (1/6) Searching for Git executable... git version 2.42.0.windows.1
⏳ (2/6) Checking local repository...
⏳ (3/6) Fetching updates...
⏳ (4/6) Switching to branch 'main'...
⏳ (5/6) Pulling updates...
⏳ (6/6) Updating submodules...
2023-10-19 08:12:00 +02:00
✔️ Switched repo 📂MyRepo to branch 'main' in 22 sec
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2023-10-19 08:12:00 +02:00
param([string]$branchName = "", [string]$repoDir = "$PWD")
2022-11-17 20:02:26 +01:00
try {
2023-10-19 08:12:00 +02:00
if ($branchName -eq "") { $branchName = Read-Host "Enter the branch name to switch to" }
if ($repoDir -eq "") { $repoDir = Read-Host "Enter the path to the local Git repository" }
2022-11-17 20:02:26 +01:00
$StopWatch = [system.diagnostics.stopwatch]::startNew()
2023-05-26 12:20:18 +02:00
Write-Host "⏳ (1/6) Searching for Git executable... " -noNewline
2022-11-17 20:02:26 +01:00
& git --version
if ($lastExitCode -ne "0") { throw "Can't execute 'git' - make sure Git is installed and available" }
2023-09-13 09:49:05 +02:00
Write-Host "⏳ (2/6) Checking local repository..."
2023-10-19 08:12:00 +02:00
$repoDir = Resolve-Path "$repoDir"
if (-not(Test-Path "$repoDir" -pathType container)) { throw "Can't access directory: $repoDir" }
2022-11-17 20:02:26 +01:00
$Result = (git status)
2023-10-19 08:12:00 +02:00
if ($lastExitCode -ne "0") { throw "'git status' in $repoDir failed with exit code $lastExitCode" }
2022-11-17 20:02:26 +01:00
if ("$Result" -notmatch "nothing to commit, working tree clean") { throw "Git repository is NOT clean: $Result" }
2023-10-19 08:12:00 +02:00
$repoDirName = (Get-Item "$repoDir").Name
2022-11-17 20:02:26 +01:00
2023-10-19 08:12:00 +02:00
"⏳ (3/6) Fetching latest updates..."
& git -C "$repoDir" fetch --all --prune --prune-tags --force
2022-11-17 20:02:26 +01:00
if ($lastExitCode -ne "0") { throw "'git fetch' failed with exit code $lastExitCode" }
2023-10-19 08:12:00 +02:00
"⏳ (4/6) Switching to branch '$branchName'..."
& git -C "$repoDir" checkout --recurse-submodules "$branchName"
if ($lastExitCode -ne "0") { throw "'git checkout $branchName' failed with exit code $lastExitCode" }
2022-11-17 20:02:26 +01:00
"⏳ (5/6) Pulling updates..."
2023-10-19 08:12:00 +02:00
& git -C "$repoDir" pull --recurse-submodules
2022-11-17 20:02:26 +01:00
if ($lastExitCode -ne "0") { throw "'git pull' failed with exit code $lastExitCode" }
"⏳ (6/6) Updating submodules..."
2023-10-19 08:12:00 +02:00
& git -C "$repoDir" submodule update --init --recursive
2022-11-17 20:02:26 +01:00
if ($lastExitCode -ne "0") { throw "'git submodule update' failed with exit code $lastExitCode" }
[int]$Elapsed = $StopWatch.Elapsed.TotalSeconds
2023-10-19 08:12:00 +02:00
"✔️ Switched repo 📂$repoDirName to branch '$branchName' in $Elapsed sec"
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-01-25 13:37:12 +01:00
*(generated by convert-ps2md.ps1 using the comment-based help of switch-branch.ps1 as of 01/25/2024 13:36:55)*