Skip to content

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

Open
vaibhavmashal wants to merge 1 commit 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 1 commit 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
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.


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

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