Conversation
Co-authored-by: mlutonsky <423848+mlutonsky@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Change force delete to safe delete for branches
git-branch-clean: use -d instead of -D for safer branch deletion
Mar 14, 2026
There was a problem hiding this comment.
Pull request overview
Updates git-branch-clean to avoid force-deleting local branches by default, reducing risk of deleting unmerged work when cleaning up branches whose remote tracking ref is gone.
Changes:
- Replace
git branch -Dwithgit branch -dfor safer deletion behavior. - Add a warning message when deletion is refused/fails, suggesting how to force-delete if desired.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
|
||
| while IFS= read -r branch; do | ||
| git branch -D "$branch" | ||
| git branch -d "$branch" || echo "warning: branch '$branch' not deleted (has unmerged commits — use 'git branch -D $branch' to force)" |
|
|
||
| while IFS= read -r branch; do | ||
| git branch -D "$branch" | ||
| git branch -d "$branch" || echo "warning: branch '$branch' not deleted (has unmerged commits — use 'git branch -D $branch' to force)" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
git branch -Dforce-deletes regardless of merge status, risking data loss on stale branches that weren't actually merged. Since the intent is to clean up branches whose remote tracking ref is gone,git branch -dis the correct default.Change
git branch -Dwithgit branch -dand add a|| echowarning on failure that tells the user how to force-delete if neededOriginal prompt
git-branch-clean: use -d instead of -D for safer branch deletion
File to modify:
git-branch-cleanProblem
The script uses
git branch -D "$branch"(force delete) to remove stale branches. This will delete a branch even if it has unmerged commits, which could result in accidental data loss. Since the intent is to clean up branches whose remote tracking ref is gone (which typically means they were merged),git branch -dis the safer default — it will refuse to delete branches with unmerged commits and print a warning instead.Fix
Change line 42 from:
git branch -D "$branch"To:
No other changes to the file.
The following is the prior conversation context from the user's chat exploration (may be truncated):
Assistant: All 10 pull requests have been submitted to mlutonsky/utils. Here's a summary of what was created:
LICENSEfile so users know they can freely use and adapt the scripts.github/workflows/shellcheck.ymlto automatically lint all shell scripts on every push and PRgit branch -D→git branch -dand adds an exit guard ongit fetch --prunepython3call instead of spawning one process per branchgit pull --allgit pull --allaftergit switch -c, which already sets up tracking[[ "$selection" =~ ^[0-9]+$ ]]guard to prevent a cryptic Bash error on non-numeric inputrotate_logfunction andMAX_LOG_LINESenv var to cap the log file at 10,000 linesgdbus callexit code directly instead of grepping stdout for the string"true"~/.local/bin/, with README and CLAUDE.md updatesYou can review them all at github.com/mlutonsky/utils/pulls and merge whichever you find useful.
User: @copilot Accepted Confirmation: Are you sure?
Assistant: All 10 pull requests have been queued with the coding agent. Here's the full list to track — you can monitor progress at github.com/mlutonsky/utils/pulls:
LICENSE(MIT, 2026, mlutonsky).github/workflows/shellcheck.yml-D→-d, adds exit guard ongit fetch --prunepython3call for all branches, not one per branchgit pull --allgit switch -c[: abc: integer expression expectedMAX_LOG_LINESenv var +rotate_logfunctionswitch_profilesuccess checkgdbusexit code instead of grepping for"true"DBUS_SESSION_BUS_ADDRESSfallbackinstall.sh~/.local/bin/in one commandThe agent is working on these now — PRs will appear in your repository as they're completed. Review and merge whichever you find useful!
User: @copilot Accepted Confirmation: Are you sure?
Assistant: ````list type="task"
data:
- title: 'Add ShellCheck CI workflow'
repository: mlutonsky/utils
url: https://github.com/copilot/tasks/pull/PR_kwDORnE_6M7KoP2B
state: queued