Matt Fenwick
Matt Fenwick

Reputation: 49085

Why doesn't git allow me to safely delete a branch?

I needed to merge two branches -- second into first and then get rid of second. Here's what I did:

Then git says:

$ git branch -d second
warning: not deleting branch 'second' that is not yet merged to
         'refs/remotes/origin/second', even though it is merged to HEAD.
error: The branch 'second' is not fully merged.
If you are sure you want to delete it, run 'git branch -D second'.

Why is this happening? I've never gotten this message after a merge before. The merge worked just fine, no conflicts. How do I safely delete the second branch?

Upvotes: 39

Views: 12203

Answers (3)

Kevin Glasson
Kevin Glasson

Reputation: 408

I think this actually happens when you have merged a branch on the remote (i.e. via a Pull Request) and deleted the remote branch.

In my experiment not deleting the remote branch and using -d locally FIRST is the solution.

Upvotes: 0

Guichi
Guichi

Reputation: 2343

Basically, the local "second" branch is not synced with the remote "second" branch, and git does not want you to lose any work.

In this situation, the update is not sure to get lost because it has already merged to "HEAD". If the "HEAD" branch is pushed to remote, there should not be a problem. However, if there is something wrong with "HEAD", there still is a chance you lose your work

To conclude, it just an extra guarantee, feel free to forcibly delete.

Upvotes: 0

Matt Fenwick
Matt Fenwick

Reputation: 49085

Based on my experiments and @knittl's and @twalberg's comments, it seems that git just wanted me to push my changes to the second branch before deleting it.

I did:

$ git checkout second
$ git push origin second
$ git checkout first
$ git branch -d second

which worked without warnings.

Upvotes: 58

Related Questions