# Git branch deletion not based on HEAD branch anymore

3 messages from 2017-04-06 to 2017-04-07. Participants: Javier Domingo Cansino, Jeff King.
Thread: https://gitlist.dev/t/45624

## Javier Domingo Cansino, 2017-04-06 20:31

Subject: Git branch deletion not based on HEAD branch anymore
Message-ID: <CALZVapk4zxn-DSdMdy2q-Z0KvsBvx4MCSB5UDVOaBrBvAqw_AA@mail.gmail.com>
URL: https://gitlist.dev/e/CALZVapk4zxn-DSdMdy2q-Z0KvsBvx4MCSB5UDVOaBrBvAqw_AA%40mail.gmail.com

```
Hello,

I have noticed that in the "latest" versions of git, `git branch -d
branch` instead of refusing to delete a branch that hasn't been merged
to HEAD, it now throws a warning and deleting the local branch if it's
present in a remote.

Example:
ˋˋˋ
> git branch -d command-runner
warning: deleting branch 'command-runner' that has been merged to
         'refs/remotes/origin/command-runner', but not yet merged to HEAD.
Deleted branch command-runner (was 1716ed5).
ˋˋˋ

After diving in git blame for a while I have realized that "lately"
refers to a commit done in 2009.

Because the change is over 7 years old, I will explain what is my use case.

The workflow I follow since I started with git is to start an exact
replica of my working environment in a personal fork. Anything deleted
locally is deleted remotely and viceversa. This allows me to move
between work and home smoothly. I also use the default `git config
push.default matching`

My typical day starts and ends pulling master from the central repo,
and `git branch -d`-ing  all the branches I have in local. git would
delete the ones that have been merged, and leave alone the ones that
have been not been merged. Then I rebase all my branches and push the
new master and rebased branches. Because all my local branches track
my personal repo, all of them get deleted if I execute my scripts.

I understand it's not possible / convenient anymore to go back, but
would it be possible to have an option such as `--merged` to support
the old usecase?

-- 
Javier Domingo Cansino

```

## Jeff King, 2017-04-07 06:38

Subject: Re: Git branch deletion not based on HEAD branch anymore
Message-ID: <20170407063808.frjjclz7ksqgmtyt@sigill.intra.peff.net>
URL: https://gitlist.dev/e/20170407063808.frjjclz7ksqgmtyt%40sigill.intra.peff.net
In-Reply-To: <CALZVapk4zxn-DSdMdy2q-Z0KvsBvx4MCSB5UDVOaBrBvAqw_AA@mail.gmail.com>

```
On Thu, Apr 06, 2017 at 10:31:55PM +0200, Javier Domingo Cansino wrote:

> I understand it's not possible / convenient anymore to go back, but
> would it be possible to have an option such as `--merged` to support
> the old usecase?

There's "branch --merged" already. And in recent versions of git, the
scriptable for-each-ref knows it, too. So you could do something like:

  git for-each-ref --format='delete %(refname)' --merged HEAD refs/heads/ |
  grep -v 'any-branches-you-want-saved' |
  git update-ref --stdin

The "--merged" option to git-branch is only used for listing. In theory
we could use it for selection in other operations, like "git branch -d
--merged". But I'm not sure how you'd tell it _not_ to delete the branch
that matches HEAD.

-Peff

```

## Javier Domingo Cansino, 2017-04-07 11:17

Subject: Re: Git branch deletion not based on HEAD branch anymore
Message-ID: <CALZVapk1nhBv04QQSV9jOpg-KQxVE8wKgV9dgwm0m+J3fxsD3g@mail.gmail.com>
URL: https://gitlist.dev/e/CALZVapk1nhBv04QQSV9jOpg-KQxVE8wKgV9dgwm0m%2BJ3fxsD3g%40mail.gmail.com
In-Reply-To: <20170407063808.frjjclz7ksqgmtyt@sigill.intra.peff.net>

```
> There's "branch --merged" already. And in recent versions of git, the
> scriptable for-each-ref knows it, too. So you could do something like:
>
>   git for-each-ref --format='delete %(refname)' --merged HEAD refs/heads/ |
>   grep -v 'any-branches-you-want-saved' |
>   git update-ref --stdin

Yeah I wanted to avoid anything else that git branch -d, as it doesn't
delete the currently checked out. I am not really familiar with
plumbing commands.

> The "--merged" option to git-branch is only used for listing. In theory
> we could use it for selection in other operations, like "git branch -d
> --merged". But I'm not sure how you'd tell it _not_ to delete the branch
> that matches HEAD.

Last time I checked git branch doesn't delete the currently checked
out branch, does it?

Adding the --merged to `git branch -d` would be awesome

Javier

```
