Git "error: The branch 'x' is not fully merged"
Here are the commands I used from the master branch
git branch experiment
git checkout experiment
Then I made some changes to my files, committed the changes, and pushed the new branch to GitHub.
git commit . -m 'changed files'
git push -u origin experi开发者_运维百科ment
Later on I decided to merge my experiment branch into the master branch.
git checkout master
git merge experiment
Finally I pushed the changes to GitHub.
git push -u origin master
All went well until I tried deleting my experiment branch using
git branch -d experiment
I got the error message:
error: The branch 'experiment' is not fully merged.
If you are sure you want to delete it, run 'git branch -D experiment'.
I'm a bit new to git, and I don't know how much more I could possibly merge the two branches. What am I missing here?
Note Wording changed in response to the commments. Thanks @slekse
That is not an error, it is a warning. It means the branch you are about to delete contains commits that are not reachable from any of: its upstream branch, or HEAD (currently checked out revision). In other words, when you might lose commits¹.
In practice it means that you probably amended, rebased (including squash merge) or filtered commits and they don't seem identical.
Therefore you could avoid the warning by checking out a branch that does contain the commits that you're about un-reference by deleting that other branch.²
You will want to verify that you in fact aren't missing any vital commits:
git log --graph --left-right --cherry-pick --oneline master...experiment
This will give you a list of any nonshared between the branches. In case you are curious, there might be a difference without --cherry-pick
and this difference could well be the reason for the warning you get:
--cherry-pick
Omit any commit that introduces the same change as another commit on the "other side" when the set of commits are limited with symmetric difference. For example, if you have two branches, A and B, a usual way to list all commits on only one side of them is with --left-right, like the example above in the description of that option. It however shows the commits that were cherry-picked from the other branch (for example, "3rd on b" may be cherry-picked from branch A). With this option, such pairs of commits are excluded from the output.
¹ they're really only garbage collected after a while, by default. Also, the git-branch
command does not check the revision tree of all branches. The warning is there to avoid obvious mistakes.
² (My preference here is to just force the deletion instead, but you might want to have the extra reassurance).
As Drew Taylor pointed out, branch deletion with -d only considers the current HEAD in determining if the branch is "fully merged". It will complain even if the branch is merged with some other branch. The error message could definitely be clearer in this regard... You can either checkout the merged branch before deleting, or just use git branch -D. The capital -D will override the check entirely.
I tried sehe's answer and it did not work.
To find the commits that have not been merged simply use:
git log feature-branch ^master --no-merges
I had this happen to me today, as I was merging my very first feature branch back into master. As some said in a thread elsewhere on SO, the trick was switching back to master before trying to delete the branch. Once in back in master, git was happy to delete the branch without any warnings.
Easiest Solution With Explanation (double checked solution) (faced the problem before)
Problem is:
1- I can't delete a branch
2- The terminal keep display a warning message that there are some commits that are not approved yet
3- knowing that I checked the master and branch and they are identical (up to date)
solution:
git checkout master
git merge branch_name
git checkout branch_name
git push
git checkout master
git branch -d branch_name
Explanation:
when your branch is connected to upstream remote branch (on Github, bitbucket or whatever), you need to merge (push) it into the master, and you need to push the new changes (commits) to the remote repo (Github, bitbucket or whatever) from the branch,
what I did in my code is that I switched to master, then merge the branch into it (to make sure they're identical on your local machine), then I switched to the branch again and pushed the updates or changes into the remote online repo using "git push".
after that, I switched to the master again, and tried to delete the branch, and the problem (warning message) disappeared, and the branch deleted successfully
Git is warning that you might lose history by deleting this branch. Even though it would not actually delete any commits right away, some or all of the commits on the branch would become unreachable if they are not part of some other branch as well.
For the branch experiment
to be “fully merged” into another branch, its tip commit must be an ancestor of the other branch’s tip, making the commits in experiment
a subset of the other branch. This makes it safe to delete experiment
, since all its commits will remain part of the repository history via the other branch. It must be “fully” merged, because it may have been merged several times already, but now have commits added since the last merge that are not contained in the other branch.
Git doesn’t check every other branch in the repository, though; just two:
- The current branch (HEAD)
- The upstream branch, if there is one
The “upstream branch” for experiment
, as in your case, is probably origin/experiment
. If experiment
is fully merged in the current branch, then Git deletes it with no complaint. If it is not, but it is fully merged in its upstream branch, then Git proceeds with a warning seeming like:
warning: deleting branch 'experiment' that has been merged
to 'refs/remotes/origin/experiment', but not yet merged to
HEAD.
Deleted branch experiment (was xxxxxxxx).
Where xxxxxxxx
indicates a commit id. Being fully merged in its upstream indicates that the commits in experiment
have been pushed to the origin repository, so that even if you lose them here, they may at least be saved elsewhere.
Since Git doesn’t check other branches, it may be safe to delete a branch because you know it is fully merged into another one; you can do this with the -D
option as indicated, or switch to that branch first and let Git confirm the fully merged status for you.
You can simply figure out :
git log --cherry master...experimental
--cherry
option is a synonym for --right-only --cherry-mark --no-merges
git-log man page said
it's useful to limit the output to the commits on our side and mark those that have been applied to the other side of a forked history with git log --cherry upstream...mybranch, similar to git cherry upstream mybranch.
FYI. --cherry-pick
omits equivalent commits but --cherry-marks
doesn't. It's useful to find rebase and force updated changes between upstream and co-working public branch
to see changes that are not merged, I did this:
git checkout experiment
git merge --no-commit master
git diff --cached
Note: This shows changes in master
that are not in experiment
.
Don't forget to:
git merge --abort
When you're done lookin.
I did not have the upstream branch on my local git. I had created a local branch from master, git checkout -b mybranch . I created a branch with bitbucket GUI on the upstream git and pushed my local branch (mybranch) to that upstream branch. Once I did a git fetch on my local git to retrieve the upstream branch, I could do a git branch -d mybranch.
I believe the flag --force
is what you are really looking for. Just use git branch -d --force <branch_name>
to delete the branch forcibly.
C:\inetpub\wwwroot\ember-roomviewer>git branch -d guided-furniture
warning: not deleting branch 'guided-furniture' that is not yet merged to
'refs/remotes/origin/guided-furniture', even though it is merged to HEAD.
error: The branch 'guided-furniture' is not fully merged.
If you are sure you want to delete it, run 'git branch -D guided-furniture'.
The solution for me was simply that the feature branch needed to be pushed up to the remote. Then when I ran:
git push origin guided-furniture
/* You might need to fetch here */
git branch -d guided-furniture
Deleted branch guided-furniture (was 1813496).
If you made a merge on Github and are seeing the error below. You need to pull (fetch and commit) the changes from the remote server before it will recognize the merge on your local. After doing this, Git will allow you to delete the branch without giving you the error.
error: The branch 'x' is not fully merged. If you are sure you want to delete it, run 'git branch -D 'x'.
The accepted answer is correct. In this answer, I will add three things:
- how this has happened (often to me)
- show an example
- how to make sure you are not missing any changes before force delete via
-D
I use Rebase and merge on GitHub as the default PR merging approach. That creates new commit (hashes) for the same changes.
For example, when I run
git log --graph --left-right --oneline add-theme-dark-2...main
on one of my project, I got this:
> fba6fce (HEAD -> main, tag: v2.0.9, origin/main) Refactored to semantic class names.
> 4e665bc Refactored to semantic class names. (1a)
....
> 8bd13a6 Added 'font-semibold' to title. (2a)
< 23f7b8a (add-theme-dark-2) Refactored to semantic class names.
< cf71814 Refactored to semantic class names. (1b)
....
< d3a774a Added 'font-semibold' to title. (2b)
(END)
Note how 1a/1b and 2a/2b have different commit hashes.
To make sure that you are not missing changes, run:
git log --graph --left-right --cherry-pick --oneline add-theme-dark-2...main
If it return a list, that each line start with "=":
= fba6fce (HEAD -> main, tag: v2.0.9, origin/main) Refactored to semantic class names.
= 4e665bc Refactored to semantic class names.
...
= 346770b Moved text size to component.
= 68cd471 Added theme-dark.
= 8bd13a6 Added 'font-semibold' to title.
it is safe to delete the branch with:
git branch -D add-theme-dark2
In my case , this happened because the branch had no commits. It had only been created from another branch.
The solution: Make 1 commit in the branch. Then I could switch to another branch and delete it, with no error.
精彩评论