tj / tj/git-extras

`git delete-merged-branches` can delete default branch such as `master`

Open
#1,132 12 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Shell
Stars
18.1k
Forks
1.2k
Avg merge
5d 17h
Merged PRs (30d)
1

Description

When running git delete-merged-branches on a non default branch, it currently deletes the default branch:

$ git switch -c InlayHintsPassFactory-mayhem
$ git delete-merged-branches
Deleted branch master (was 81901fca2).

It's not a big issue, as it can be recovered by switching to master again, looking at the ref log if needed. But this might be something to fix.

It's possible to get the default branch of the remote named origin

$ git rev-parse --abbrev-ref origin/HEAD
origin/master

If the remote are not named origin this could fail

$ git remote show
bric3
upstream
$ git rev-parse --abbrev-ref origin/HEAD
origin/HEAD
fatal: ambiguous argument 'origin/HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

And not all remote have a default branch

$ git rev-parse --abbrev-ref bric3/HEAD
bric3/HEAD
fatal: ambiguous argument 'bric3/HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
$ ls .git/refs/remotes/bric3/HEAD
".git/refs/remotes/bric3/HEAD": No such file or directory (os error 2)
$ git rev-parse --abbrev-ref upstream/HEAD
upstream/master
$ ls .git/refs/remotes/upstream/HEAD
.git/refs/remotes/upstream/HEAD

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the implementation of git delete-merged-branches and reproduce the reported deletion from a non-default branch. Check the behavior with origin, non-origin remotes, and remotes without a default branch; it is done when the repository's default branch is not deleted while eligible merged branches still are.

Written by the indexing model from the issue text.

Assessment

Tech stack
git, shell
Domain
cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.