Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

git

branch

merge

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

  • git cloned the project to get a fresh copy
  • git checkout --track origin/second, made some changes, and committed
  • git checkout --track origin/first, made some changes, and committed
  • git merge second (git says "merge made by recursive")
  • git branch -d second

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?

like image 617
Matt Fenwick Avatar asked Sep 19 '12 13:09

Matt Fenwick


People also ask

Why can't I delete my git branch?

If the branch contains unmerged changes, though, Git will refuse to delete it. If you're sure you want to do it, you'll have to force the deletion by replacing the -d parameter with an uppercase D: It's like you're pushing—sending—the order to delete the branch to the remote repository.

How do you delete a branch safely?

Deleting a branch LOCALLY Delete a branch with git branch -d <branch> . The -d option will delete the branch only if it has already been pushed and merged with the remote branch. Use -D instead if you want to force the branch to be deleted, even if it hasn't been pushed or merged yet.

Is it safe to delete a local git branch?

It is safe to delete your local branch after you pushed your changes to your own remote repository. The pull request is unrelated to this, because it is simply a request to the maintainers of the original repository to merge your changes back into their code base.

How we can delete branch in git?

The command to delete a local git branch can take one of two forms: git branch –delete old-branch. git branch -d old-branch.


1 Answers

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.

like image 165
Matt Fenwick Avatar answered Oct 19 '22 06:10

Matt Fenwick