threads / discuss / 45624

Git branch deletion not based on HEAD branch anymore

Subject: Git branch deletion not based on HEAD branch anymore

## tl;dr

3 messages between Apr 6, 2017 and Apr 7, 2017.

replies: 2people: 2as markdown or json

Javier Domingo Cansino· Apr 6, 2017, 20:31 UTC · lore
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· Apr 7, 2017, 06:38 UTC · re: Javier Domingo Cansino · lore

Re: Git branch deletion not based on HEAD branch anymore

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· Apr 7, 2017, 11:17 UTC · re: Jeff King · lore

Re: Git branch deletion not based on HEAD branch anymore

Show 6 quoted lines
> 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

← back to recent threads