Skip to content

💫 Toolkit to help you get started with SDD or any other process!

fix(scripts): improve git branch creation error handling (#2089)

Adam Boczek4178b6182894
diff --git a/scripts/bash/create-new-feature.sh b/scripts/bash/create-new-feature.sh
index 36ea53799..f9ba9545d 100644
--- a/scripts/bash/create-new-feature.sh
+++ b/scripts/bash/create-new-feature.sh
@@ -327,12 +327,17 @@ SPEC_FILE="$FEATURE_DIR/spec.md"
if [ "$DRY_RUN" != true ]; then
if [ "$HAS_GIT" = true ]; then
- if ! git checkout -b "$BRANCH_NAME" 2>/dev/null; then
+ branch_create_error=""
+ if ! branch_create_error=$(git checkout -q -b "$BRANCH_NAME" 2>&1); then
+ current_branch="$(git rev-parse --abbrev-ref HEAD 2>/dev/null || true)"
# Check if branch already exists
if git branch --list "$BRANCH_NAME" | grep -q .; then
if [ "$ALLOW_EXISTING" = true ]; then
- # Switch to the existing branch instead of failing
- if ! git checkout "$BRANCH_NAME" 2>/dev/null; then
+ # If we're already on the branch, continue without another checkout.
+ if [ "$current_branch" = "$BRANCH_NAME" ]; then
+ :
+ # Otherwise switch to the existing branch instead of failing.
+ elif ! git checkout "$BRANCH_NAME" 2>/dev/null; then
>&2 echo "Error: Failed to switch to existing branch '$BRANCH_NAME'. Please resolve any local changes or conflicts and try again."
exit 1
fi
@@ -344,7 +349,12 @@ if [ "$DRY_RUN" != true ]; then
exit 1
fi
else
- >&2 echo "Error: Failed to create git branch '$BRANCH_NAME'. Please check your git configuration and try again."
+ >&2 echo "Error: Failed to create git branch '$BRANCH_NAME'."
+ if [ -n "$branch_create_error" ]; then
+ >&2 printf '%s\n' "$branch_create_error"
+ else
+ >&2 echo "Please check your git configuration and try again."
+ fi
exit 1
fi
fi
diff --git a/scripts/powershell/create-new-feature.ps1 b/scripts/powershell/create-new-feature.ps1
index 2cfa35139..3e7e525b8 100644
--- a/scripts/powershell/create-new-feature.ps1
+++ b/scripts/powershell/create-new-feature.ps1
@@ -293,25 +293,33 @@ $specFile = Join-Path $featureDir 'spec.md'
if (-not $DryRun) {
if ($hasGit) {
$branchCreated = $false
+ $branchCreateError = ''
try {
- git checkout -q -b $branchName 2>$null | Out-Null
+ $branchCreateError = git checkout -q -b $branchName 2>&1 | Out-String
if ($LASTEXITCODE -eq 0) {
$branchCreated = $true
}
} catch {
- # Exception during git command
+ $branchCreateError = $_.Exception.Message
}
if (-not $branchCreated) {
+ $currentBranch = ''
+ try { $currentBranch = (git rev-parse --abbrev-ref HEAD 2>$null).Trim() } catch {}
# Check if branch already exists
$existingBranch = git branch --list $branchName 2>$null
if ($existingBranch) {
if ($AllowExistingBranch) {
- # Switch to the existing branch instead of failing
- git checkout -q $branchName 2>$null | Out-Null
- if ($LASTEXITCODE -ne 0) {
- Write-Error "Error: Branch '$branchName' exists but could not be checked out. Resolve any uncommitted changes or conflicts and try again."
- exit 1
+ # If we're already on the branch, continue without another checkout.
+ if ($currentBranch -eq $branchName) {
+ # Already on the target branch — nothing to do
+ } else {
+ # Otherwise switch to the existing branch instead of failing.
+ git checkout -q $branchName 2>$null | Out-Null
+ if ($LASTEXITCODE -ne 0) {
+ Write-Error "Error: Branch '$branchName' exists but could not be checked out. Resolve any uncommitted changes or conflicts and try again."
+ exit 1
+ }
}
} elseif ($Timestamp) {
Write-Error "Error: Branch '$branchName' already exists. Rerun to get a new timestamp or use a different -ShortName."
@@ -321,7 +329,11 @@ if (-not $DryRun) {
exit 1
}
} else {
- Write-Error "Error: Failed to create git branch '$branchName'. Please check your git configuration and try again."
+ if ($branchCreateError) {
+ Write-Error "Error: Failed to create git branch '$branchName'.`n$($branchCreateError.Trim())"
+ } else {
+ Write-Error "Error: Failed to create git branch '$branchName'. Please check your git configuration and try again."
+ }
exit 1
}
}