PowerShell/docs/switch-branch.md

130 lines
4.5 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
2024-08-15 09:51:46 +02:00
This PowerShell script switches to the given branch in a Git repository and also updates the 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
2024-03-27 17:36:59 +01:00
PS> ./switch-branch.ps1 [[-branchName] <String>] [[-pathToRepo] <String>] [<CommonParameters>]
2021-11-08 21:36:42 +01:00
2023-10-19 08:12:00 +02:00
-branchName <String>
2024-05-19 10:25:56 +02:00
Specifies the Git branch name to switch to
2021-11-08 21:36:42 +01:00
Required? false
Position? 1
Default value
Accept pipeline input? false
Accept wildcard characters? false
2024-03-27 17:36:59 +01:00
-pathToRepo <String>
Specifies the file 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
2024-08-15 09:51:46 +02:00
PS> ./switch-branch main
2024-03-27 17:36:59 +01:00
⏳ (1/6) Searching for Git executable... git version 2.43.0.windows.1
2024-08-15 09:51:46 +02:00
⏳ (2/6) Checking local repository... C:\Repos\rust
2024-05-19 10:25:56 +02:00
⏳ (3/6) Fetching remote updates...
2023-09-13 09:49:05 +02:00
⏳ (4/6) Switching to branch 'main'...
2024-05-19 10:25:56 +02:00
⏳ (5/6) Pulling remote updates...
2023-09-13 09:49:05 +02:00
⏳ (6/6) Updating submodules...
2024-05-19 10:25:56 +02:00
✔️ Switched 📂rust repo to 'main' branch in 22s.
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
2024-08-15 09:51:46 +02:00
This PowerShell script switches to the given branch in a Git repository and also updates the submodules.
2023-10-19 08:12:00 +02:00
.PARAMETER branchName
2024-05-19 10:25:56 +02:00
Specifies the Git branch name to switch to
2024-03-27 17:36:59 +01:00
.PARAMETER pathToRepo
Specifies the file path to the local Git repository
2022-11-17 20:02:26 +01:00
.EXAMPLE
2024-08-15 09:51:46 +02:00
PS> ./switch-branch main
2024-03-27 17:36:59 +01:00
⏳ (1/6) Searching for Git executable... git version 2.43.0.windows.1
2024-08-15 09:51:46 +02:00
⏳ (2/6) Checking local repository... C:\Repos\rust
2024-05-19 10:25:56 +02:00
⏳ (3/6) Fetching remote updates...
2023-09-13 09:49:05 +02:00
⏳ (4/6) Switching to branch 'main'...
2024-05-19 10:25:56 +02:00
⏳ (5/6) Pulling remote updates...
2023-09-13 09:49:05 +02:00
⏳ (6/6) Updating submodules...
2024-05-19 10:25:56 +02:00
✔️ Switched 📂rust repo to 'main' branch in 22s.
2022-11-17 20:02:26 +01:00
.LINK
https://github.com/fleschutz/PowerShell
.NOTES
Author: Markus Fleschutz | License: CC0
#>
2024-03-27 17:36:59 +01:00
param([string]$branchName = "", [string]$pathToRepo = "$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" }
2022-11-17 20:02:26 +01:00
2024-03-27 17:36:59 +01:00
$stopWatch = [system.diagnostics.stopwatch]::startNew()
2022-11-17 20:02:26 +01:00
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" }
2024-08-15 09:51:46 +02:00
Write-Host "⏳ (2/6) Checking local repository... $pathToRepo"
2024-03-27 17:36:59 +01:00
if (-not(Test-Path "$pathToRepo" -pathType container)) { throw "Can't access repo folder: $pathToRepo" }
2024-05-19 10:25:56 +02:00
$result = (git -C "$pathToRepo" status)
2024-03-27 17:36:59 +01:00
if ($lastExitCode -ne "0") { throw "'git status' in $pathToRepo failed with exit code $lastExitCode" }
if ("$result" -notmatch "nothing to commit, working tree clean") { throw "Git repository is NOT clean: $result" }
$repoDirName = (Get-Item "$pathToRepo").Name
2022-11-17 20:02:26 +01:00
2024-08-15 09:51:46 +02:00
Write-Host "⏳ (3/6) Fetching remote updates... " -noNewline
& git -C "$pathToRepo" remote get-url origin
if ($lastExitCode -ne "0") { throw "'git remote get-url origin' failed with exit code $lastExitCode" }
2024-03-27 17:36:59 +01:00
& git -C "$pathToRepo" 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'..."
2024-03-27 17:36:59 +01:00
& git -C "$pathToRepo" checkout --recurse-submodules "$branchName"
2023-10-19 08:12:00 +02:00
if ($lastExitCode -ne "0") { throw "'git checkout $branchName' failed with exit code $lastExitCode" }
2022-11-17 20:02:26 +01:00
2024-05-19 10:25:56 +02:00
"⏳ (5/6) Pulling remote updates..."
2024-03-27 17:36:59 +01:00
& git -C "$pathToRepo" 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..."
2024-03-27 17:36:59 +01:00
& git -C "$pathToRepo" 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" }
2024-03-27 17:36:59 +01:00
[int]$elapsed = $stopWatch.Elapsed.TotalSeconds
2024-05-19 10:25:56 +02:00
"✔️ Switched 📂$repoDirName repo to '$branchName' branch in $($elapsed)s."
2022-11-17 20:02:26 +01:00
exit 0 # success
} catch {
2024-08-15 09:51:46 +02:00
"⚠️ Error: $($Error[0]) in script line $($_.InvocationInfo.ScriptLineNumber)"
2022-11-17 20:02:26 +01:00
exit 1
}
2022-11-17 20:05:34 +01:00
```
2022-11-17 20:02:26 +01:00
2024-08-15 09:51:46 +02:00
*(generated by convert-ps2md.ps1 using the comment-based help of switch-branch.ps1 as of 08/15/2024 09:50:54)*