Skip to content

fix(branch): prevent git delete-merged-branches from deleting default branch (#1132) - #1269

Open
vaibhavmashal wants to merge 3 commits into
tj:masterfrom
vaibhavmashal:fix/prevent-deleting-default-branch-1132
Open

fix(branch): prevent git delete-merged-branches from deleting default branch (#1132)#1269
vaibhavmashal wants to merge 3 commits into
tj:masterfrom
vaibhavmashal:fix/prevent-deleting-default-branch-1132

Conversation

@vaibhavmashal

Copy link
Copy Markdown

Summary

Fixes #1132

When running \git delete-merged-branches\ on a repository where the default/primary branch is \master\ (or when \origin/HEAD\ is set) from a non-default branch, \git_extra_default_branch\ previously fell back to \main\ (or whatever \init.defaultBranch\ was set to globally), which resulted in \master\ being treated as a merged branch and deleted.

Changes

  • Updated \git_extra_default_branch()\ in \helper/git-extra-utility\ to:
    1. Respect \git config --get git-extras.default-branch`n 2. Inspect
      efs/remotes/origin/HEAD\ symbolic ref
    2. Inspect \git config --get init.defaultBranch`n 4. Check if \main\ exists locally or remotely
    3. Check if \master\ exists locally or remotely
    4. Fall back to \main`n- Updated \�in/git-delete-merged-branches, \�in/git-show-merged-branches, and \�in/git-show-unmerged-branches\ to explicitly protect the detected default branch as well as primary branches (\main, \master, \ runk, \svn).

Copilot AI lite review requested due to automatic review settings August 29, 2026 19:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@hyperupcall

Copy link
Copy Markdown
Collaborator

Woah this diff is huge, perhaps there was a mistake in the history of the branch of this PR?

@vaibhavmashal
vaibhavmashal force-pushed the fix/prevent-deleting-default-branch-1132 branch from b323130 to d0ca537 Compare August 30, 2026 17:13
@vaibhavmashal

Copy link
Copy Markdown
Author

Good catch @hyperupcall! My initial branch history had diverged from upstream. I've rebased directly onto \upstream/master\ — the PR diff is now clean with just the intended fix (+14 / -4 across \git-delete-merged-branches, \git-show-merged-branches, \git-show-unmerged-branches, and \git-extra-utility).

@spacewander spacewander left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Let's add test in https://github.com/tj/git-extras/tree/main/tests to cover the change

Comment thread helper/git-extra-utility Outdated
init_default_branch=$(git config --get init.defaultBranch)
if [ -n "$extras_default_branch" ]; then
echo "$extras_default_branch"
elif [ -n "$origin_head" ]; then

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This hurts repo uses own or other remote names. We should append it after the existing init_default_branch branch.

Comment thread bin/git-show-merged-branches Outdated

git branch --no-color --merged | grep -v "\*" | grep -v "$(git_extra_default_branch)" | tr -d ' '
default_branch=$(git_extra_default_branch)
git branch --no-color --merged | grep -v "\*" | tr -d ' ' | grep -vE "^(${default_branch}|main|master|trunk|svn)$"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Maybe we should use Igrep -Fvx with separate -e arguments to avoid regex char in the branch name, like release/foo+bar

@vaibhavmashal

Copy link
Copy Markdown
Author

Added unit tests in \ ests/test_delete_merged_branches.py\ to cover \git delete-merged-branches, \git show-merged-branches, and \git show-unmerged-branches.

The tests verify that:

  • Merged feature branches are properly deleted.
  • The default branch (\master\ / \main) is protected and preserved.
  • Unmerged feature branches remain intact.

Pushed in commit \ca67303.

@vaibhavmashal
vaibhavmashal force-pushed the fix/prevent-deleting-default-branch-1132 branch from 08ff251 to 7eb6eed Compare August 31, 2026 15:41
@vaibhavmashal

Copy link
Copy Markdown
Author

Hi @spacewander,

Thank you for the review and suggestions!

I have addressed all the feedback in commit 7eb6eed:

  1. Remote vs local default branch resolution: Updated git_extra_default_branch() in helper/git-extra-utility so init.defaultBranch is evaluated before falling back to origin/HEAD.
  2. Special characters in branch names: Updated bin/git-delete-merged-branches, bin/git-show-merged-branches, and bin/git-show-unmerged-branches to use grep -Fvx -e "$default_branch" -e "main" -e "master" -e "trunk" -e "svn" instead of regex matching. This prevents branch names with special characters (such as +, ., etc.) from breaking pattern matching.
  3. Unit Tests: Added test coverage in tests/test_delete_merged_branches.py (including testing branch names containing regex characters like release/foo+bar), all passing cleanly.

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.

4 participants