142

Just want to get a better understanding of the warning message after I deleted a local branch

warning: deleting branch 'old_branch' that has been merged to 'refs/remotes/origin/old_branch', but not yet merged to HEAD.

Martin Thoma
  • 124,992
  • 159
  • 614
  • 958
user1322228
  • 1,449
  • 2
  • 9
  • 5
  • 64
    if you merge --squash your old_branch you'll get this warning always when deleting it locally. When squashing your index is reset hence does not match old_branch HEAd – Antoine Wils Jul 03 '15 at 09:07
  • 3
    While the other answers are correct, Antoine's comment was the reason at least in my case. – Squrppi Mar 05 '20 at 11:09
  • 4
    @AntoineWils, this should an answer. – 0TTT0 Sep 17 '22 at 17:53

6 Answers6

103

This is just warning you that you have changes pushed to the branch on origin, but they are not merged into the currently checked out branch, so you are only deleting it locally.

It is warning you that you no longer have a local copy of that branch, but it exists in origin

If you want to delete the remote branch as well, use git push --delete origin old_branch

radix
  • 352
  • 1
  • 2
  • 14
cjhveal
  • 5,668
  • 2
  • 28
  • 38
  • 11
    Yeah, `master` is not involved in this. You mean the current branch, rather. `;)` – Fernando Espinosa Aug 31 '16 at 18:43
  • 3
    I get this warning even if I do a `git branch -d branch_name` which is supposed to only delete the branch locally. Why is the warning needed in this case? – Akshay Damle Nov 15 '17 at 08:55
  • 4
    But origin/old_branch is merged into origin/master. Later I did git pull origin on master branch. Still same error. Isn't this bug in GH ? – vikramvi Mar 08 '18 at 11:06
  • I'm getting: error: failed to push some refs to 'https://github.com/repo/repo.git' – 629060311 Mar 31 '22 at 21:10
45

Assuming you currently have master checked out, it means the changes made in old_branch aren't present in master. However, they are present in old_branch on origin.

Karl Bielefeldt
  • 47,314
  • 10
  • 60
  • 94
  • 8
    Yep. This is the correct answer! "but not yet merged to `HEAD`", the `HEAD` here reference the local master branch's HEAD. – Devy Sep 10 '15 at 22:09
  • 6
    But origin/old_branch is merged into origin/master. Later I did git pull origin on master branch. Still same error. Isn't this bug in GH ? – vikramvi Mar 08 '18 at 11:07
  • 3
    @vikramvi Assuming you have `master` checked out locally, it can still occur if the SHA of any of the commits has changed; i.e. you rebased `origin/old_branch` onto `origin/master`. Even if it was a fast-forward, this will generate a new SHA for each new commit from `origin/old_branch`, causing git to see the original SHA's in your local `old_branch` as unmerged after pulling the changes to your local `master` branch. You can see [this answer](https://stackoverflow.com/a/48626935/711807) and [this answer](https://stackoverflow.com/a/45781789/711807) for why this occurs. –  Jul 30 '18 at 00:09
10

This means your local branch old_branch is up to date with remote branch old_branch on remote origin but it is not merged to the branch master which is considered to be the main branch in the repo.

It is just a precaution from git. It gives you a hint: maybe you did your job in the topic-branch and forget to merge it to the main branch?


update

Git warns you from losing your changes. For example if you do not have your old_branch on the master git then don't allow you to even delete branch that is unmerged to the master (well it allow, but with key -D which is force-delete option).

Max Komarychev
  • 2,836
  • 1
  • 21
  • 32
  • 9
    Not necessarily `master`, but the current `HEAD`. – Frozen Flame Apr 10 '15 at 04:20
  • So the -D is when you have not shared the branch being deleted (really it's commits) with any remote branch. While the warning is to inform you that you have shared the branch (commits) with the remote but it just is not merged into HEAD. HEAD is HEAD, remote/refs/HEAD is not the same. But please correct me if I am wrong regarding the differences between remote HEAD and local HEAD. – Eric Jan 05 '17 at 15:32
9

To add to the other answers, this can also mean that the change might be merged to master, just that your local copy of master does not reflect it yet. Either ways this just informs you that the local copy of your master does not have the changes you pushed on origin. Merged/Not merged...maybe,maybe not

Mithun Nair
  • 450
  • 7
  • 14
7

Several answers here are completely correct, but seem to have not fully cleared up the question. So I will try another attempt.

OP Question Paraphrased

After I perform a Pull Request and remove a branch on GitHub/Bitbucket/etc. automatically via completing the PR, I am then seeing this error when deleting the branch locally: warning: deleting branch 'old_branch' that has been merged to 'refs/remotes/origin/old_branch', but not yet merged to HEAD.

Understanding Timeline

GitHub, or whatever remote repository, has no knowledge of the local state of your machine.

When a pull request is completed and it offers to remove the original PR branch, it has no knowledge of your local branch.

At this point, you delete your local branch. Your local repo/computer knows this timeline:

  1. myfeature ==> origin/myfeature (myfeature has been pushed up to remote repo)
  2. then myfeature is deleted
  3. So origin/myfeature still exists even though it has no local representation

Of course, this is not true, because the origin/myfeature branch was destroyed when the PR was completed, but your local computer does not know this. So, Git gives you the warning.

Shouldn't GitHub/BitBucket/etc. Make This Easier?

Because the

PR Complete ==> Remote Branch Removed

paradigm is so common, it would be nice if maybe the remote branch somehow informed the local branch of this occurrence. But Git has done remarkably well with "one-directional communication" where you only request information but never send unrequested information. "Bi-directional" communication in one area would likely make people want it in other areas, too, and soon we're back to Git looking more like SVN or any of the other "central repository" paradigms that were too brittle to succeed. Someone who knows more than me can probably elaborate better all the problems this "two-way communication" would cause.

Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
  • 2
    A further note: it might be worth reading up on [`git prune`](https://www.git-tower.com/learn/git/faq/cleanup-remote-branches-with-git-prune/), as it is a simple way to clear up any "extra junk" when you have pointers to remote branches that no longer work. – Mike Williamson Mar 09 '21 at 19:22
0

this means you changes for this branch is merged in remote(main/master) but not in local(main/master).

to avoid theses warning, must take checkout to main/master and then take pull.