Git “error: The branch 'x' is not fully merged”

前端 未结 12 2027
忘了有多久
忘了有多久 2020-12-04 04:34

Here are the commands I used from the master branch

git branch experiment
git checkout experiment

Then I made some changes to my files, commi

12条回答
  •  独厮守ぢ
    2020-12-04 05:05

    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:

    1. The current branch (HEAD)
    2. 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.

提交回复
热议问题