Менеджер git stash
Показывает список stash, умеет показать diff и применить или удалить запись только после подтверждения.
Что делает скрипт
Скрипт выводит stash с датой и сообщением, а действия Show, Apply, Pop и Drop выбираются флагом Action. Apply и Pop требуют подтверждения через ShouldProcess. Drop дополнительно требует Force и не выполняется для несуществующего индекса.
- Drop удаляет запись stash без возможности восстановления. Нужен явный -Force.
Быстрый старт
powershell -File Git-StashManager.ps1 -Path . -Action List
Скачайте файл, откройте терминал в папке со скриптом и выполните команду. Запускайте в PowerShell 5.1 или PowerShell 7.
Другие варианты
powershell -File Git-StashManager.ps1 -Path .powershell -File Git-StashManager.ps1 -Path . -Action Show -Index 0powershell -File Git-StashManager.ps1 -Path . -Action Apply -Index 0
Что понадобится
Встроен в Windows 10/11 (PowerShell 5.1)
winget install Git.Git
Git-StashManager.ps1
[CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')]
param(
[string]$Path = ".",
[ValidateSet('List', 'Show', 'Apply', 'Pop', 'Drop')]
[string]$Action = 'List',
[ValidateRange(0, 1000)]
[int]$Index = 0,
[switch]$Force
)
$ErrorActionPreference = 'Stop'
if (-not (Get-Command git -ErrorAction SilentlyContinue)) {
throw "git not found in PATH"
}
$repo = (Resolve-Path -LiteralPath $Path -ErrorAction Stop).Path
$null = & git -C $repo rev-parse --is-inside-work-tree 2>$null
if ($LASTEXITCODE -ne 0) {
throw "Not a git repository: $repo"
}
$format = '%gd|%ci|%cr|%gs'
$lines = @(& git -C $repo stash list "--date=iso" "--format=$format" 2>$null)
$rows = @()
foreach ($line in $lines) {
if ([string]::IsNullOrWhiteSpace($line)) {
continue
}
$parts = $line -split '\|', 4
$dateText = if ($parts.Count -gt 1) { $parts[1].Trim() } else { '' }
$ageText = if ($parts.Count -gt 2) { $parts[2].Trim() } else { '' }
$messageText = if ($parts.Count -gt 3) { $parts[3].Trim() } else { '' }
$rows += [pscustomobject]@{
Ref = $parts[0].Trim()
Date = $dateText
Age = $ageText
Message = $messageText
}
}
if ($Action -eq 'List') {
if ($rows.Count -eq 0) {
Write-Host "No stashes."
exit 0
}
$rows | Format-Table -AutoSize
exit 0
}
if ($rows.Count -eq 0) {
throw "No stashes found."
}
if ($Index -ge $rows.Count) {
throw "Stash index $Index is out of range. Available: 0..$($rows.Count - 1)"
}
$stashRef = "stash@{$Index}"
switch ($Action) {
'Show' {
& git -C $repo stash show -p $stashRef
exit $LASTEXITCODE
}
'Apply' {
if ($PSCmdlet.ShouldProcess("stash $stashRef", "Apply")) {
& git -C $repo stash apply $stashRef
exit $LASTEXITCODE
}
}
'Pop' {
if ($PSCmdlet.ShouldProcess("stash $stashRef", "Apply and remove")) {
& git -C $repo stash pop $stashRef
exit $LASTEXITCODE
}
}
'Drop' {
if (-not $Force) {
throw "Drop requires -Force because the stash entry will be deleted."
}
if ($PSCmdlet.ShouldProcess("stash $stashRef", "Drop permanently")) {
& git -C $repo stash drop $stashRef
exit $LASTEXITCODE
}
}
}
Скрипты работают с файлами и настройками системы. Перед запуском прочитайте код и проверьте пути. Особенно если скрипт удаляет, перезаписывает или перемещает данные.