Skip to content

git-branch-clean: use -d instead of -D for safer branch deletion#9

Merged
mlutonsky merged 2 commits intomainfrom
copilot/git-branch-clean-use-d-instead-of-d
Mar 14, 2026
Merged

git-branch-clean: use -d instead of -D for safer branch deletion#9
mlutonsky merged 2 commits intomainfrom
copilot/git-branch-clean-use-d-instead-of-d

Conversation

Copy link
Copy Markdown

Copilot AI commented Mar 14, 2026

git branch -D force-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 -d is the correct default.

Change

  • Replace git branch -D with git branch -d and add a || echo warning on failure that tells the user how to force-delete if needed
# Before
git branch -D "$branch"

# After
git branch -d "$branch" || echo "warning: branch '$branch' not deleted (has unmerged commits — use 'git branch -D $branch' to force)"
Original prompt

git-branch-clean: use -d instead of -D for safer branch deletion

File to modify: git-branch-clean

Problem

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 -d is 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:

  git branch -d "$branch" || echo "warning: branch '$branch' not deleted (has unmerged commits — use 'git branch -D $branch' to force)"

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:

# PR Title What it does
1 Add MIT license Adds a LICENSE file so users know they can freely use and adapt the scripts
2 Add ShellCheck CI workflow Adds .github/workflows/shellcheck.yml to automatically lint all shell scripts on every push and PR
3 git-branch-clean: safer branch deletion and fetch error handling Switches git branch -Dgit branch -d and adds an exit guard on git fetch --prune
4 git-co: fix expensive per-branch python3 Levenshtein loop Batches all branch names into a single python3 call instead of spawning one process per branch
5 git-co: remove unnecessary git pull --all Removes the redundant git pull --all after git switch -c, which already sets up tracking
6 git-co: validate branch selection input is numeric Adds [[ "$selection" =~ ^[0-9]+$ ]] guard to prevent a cryptic Bash error on non-numeric input
7 idle-power-manager: add log rotation Adds a rotate_log function and MAX_LOG_LINES env var to cap the log file at 10,000 lines
8 idle-power-manager: fix fragile switch_profile success detection Checks the gdbus call exit code directly instead of grepping stdout for the string "true"
9 idle-power-manager: add DBUS_SESSION_BUS_ADDRESS fallback Exports a fallback socket path so the script works reliably as a systemd user service
10 Add install.sh Adds a one-shot install script that symlinks all scripts to ~/.local/bin/, with README and CLAUDE.md updates

You 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:

data:
    - title: 'Add MIT license'
      repository: mlutonsky/utils
      url: https://github.com/copilot/tasks/pull/PR_kwDORnE_6M7KoO34
      state: queued
# PR What it does
1 Add MIT license Adds LICENSE (MIT, 2026, mlutonsky)
2 Add ShellCheck CI workflow Adds .github/workflows/shellcheck.yml
3 git-branch-clean: safer deletion + fetch guard -D-d, adds exit guard on git fetch --prune
4 git-co: fix expensive Levenshtein loop One python3 call for all branches, not one per branch
5 git-co: remove git pull --all Removes the unintentional pull-all after git switch -c
6 git-co: validate selection is numeric Guards against [: abc: integer expression expected
7 idle-power-manager: add log rotation Adds MAX_LOG_LINES env var + rotate_log function
8 idle-power-manager: fix switch_profile success check Checks gdbus exit code instead of grepping for "true"
9 idle-power-manager: add DBUS_SESSION_BUS_ADDRESS fallback Ensures session DBus works under systemd user service
10 Add install.sh Symlinks all scripts to ~/.local/bin/ in one command

The 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


Assistant: All 10 pull requests have been submitted to the coding agent! Here's the full tracking list:

````list type="task"
data:
    - title: 'Add MIT license'
      repository: mlutonsky/utils
      url: https://github.com/copilot/tasks/pull/PR_kwDORnE_6M7KoO34
      state: queued
    - title: 'Add ShellCheck CI workflow'
      repository: mlutonsky/ut...

</details>



<!-- START COPILOT CODING AGENT SUFFIX -->

*This pull request was created from Copilot chat.*
>

<!-- START COPILOT CODING AGENT TIPS -->
---

📍 Connect Copilot coding agent with [Jira](https://gh.io/cca-jira-docs), [Azure Boards](https://gh.io/cca-azure-boards-docs) or [Linear](https://gh.io/cca-linear-docs) to delegate work to Copilot in one click without leaving your project management tool.

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
Copilot AI requested a review from mlutonsky March 14, 2026 18:29
@mlutonsky mlutonsky marked this pull request as ready for review March 14, 2026 18:31
Copilot AI review requested due to automatic review settings March 14, 2026 18:31
@mlutonsky mlutonsky merged commit 6006728 into main Mar 14, 2026
3 checks passed
@mlutonsky mlutonsky deleted the copilot/git-branch-clean-use-d-instead-of-d branch March 14, 2026 18:31
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 -D with git branch -d for 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)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants