git Git和“分支'x'未完全合并”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7548926/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Git and "The branch 'x' is not fully merged" Error
提问by mellowsoon
Here are the commands I used from the master branch
这是我从 master 分支使用的命令
git branch experiment
git checkout experiment
Then I made some changes to my files, committed the changes, and pushed the new branch to GitHub.
然后我对我的文件进行了一些更改,提交了更改,并将新分支推送到了 GitHub。
git commit . -m 'changed files'
git push -u origin experiment
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.
最后我将更改推送到 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.
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?
我收到错误消息,error: The branch 'experiment' is not fully merged.
我对 git 有点陌生,我不知道我还能合并这两个分支多少。我在这里缺少什么?
回答by sehe
NoteWording 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 commits1.
注意措辞根据评论进行了更改。谢谢@slekse
这不是错误,而是警告。这意味着您要删除的分支包含无法从以下任何一个访问的提交:其上游分支或 HEAD(当前检出的修订)。换句话说,什么时候你可能会丢失 commits1。
In practice it means that you probably amended, rebased or filtered commits and they don't seemidentical.
在实践中,这意味着您可能修改、重新定位或过滤了提交,并且它们看起来并不相同。
Therefore you could avoid the warningby checking out a branch that does contain the commits that you're about un-referenceby deleting that other branch.2
因此,您可以通过删除其他分支来检出包含您要取消引用的提交的分支来避免警告。2
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
而这种差异很可能是您收到警告的原因:
--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.
--cherry-pick
当提交集受限于对称差异时,省略任何引入与“另一端”的另一个提交相同的更改的提交。例如,如果您有两个分支,A 和 B,通常只列出其中一侧的所有提交的方法是使用 --left-right,就像上面该选项描述中的示例一样。然而,它显示了从另一个分支中挑选出来的提交(例如,“b 上的第三个”可能是从分支 A 中挑选出来的)。使用此选项,此类提交对将从输出中排除。
1 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.
1 默认情况下,它们实际上只是在一段时间后进行垃圾收集。此外,该git-branch
命令不会检查所有分支的修订树。警告是为了避免明显的错误。
2 (My preference here is to just force the deletion instead, but you might want to have the extra reassurance).
2(我的偏好是强制删除,但您可能希望获得额外的保证)。
回答by drwowe
As Drew Taylor pointed out, branch deletion with -d only considers the currentHEAD in determining if the branch is "fully merged". It will complain even if the branch ismerged 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.
正如 Drew Taylor 指出的那样,使用 -d 删除分支只考虑当前的HEAD,以确定分支是否“完全合并”。如果分支,它会抱怨,甚至是与其他一些分支合并。在这方面错误消息肯定会更清楚......您可以在删除之前签出合并的分支,或者只使用 git branch -D。大写 -D 将完全覆盖检查。
回答by qwertzguy
I tried sehe's answer and it did not work.
我试过sehe的答案,但没有用。
To find the commits that have not been merged simply use:
要查找尚未合并的提交,只需使用:
git log feature-branch ^master --no-merges
回答by Drew Taylor
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.
我今天遇到了这种情况,因为我正在将我的第一个功能分支合并回 master。正如一些人在 SO 其他地方的一个线程中所说的那样,诀窍是在尝试删除分支之前切换回 master。回到 master 后,git 很乐意在没有任何警告的情况下删除分支。
回答by troore
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.
Git 警告您删除此分支可能会丢失历史记录。即使它实际上不会立即删除任何提交,但如果分支上的部分或全部提交不属于其他某个分支,那么它们将变得无法访问。
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.
对于experiment
要“完全合并”到另一个分支中的分支,其提示提交必须是另一个分支提示的祖先,从而在另一个分支的experiment
子集中进行提交。这使得 delete 变得安全experiment
,因为它的所有提交都将通过另一个分支保留在存储库历史记录中。它必须是“完全”合并的,因为它可能已经被合并了好几次,但现在已经添加了自上次合并以来未包含在另一个分支中的提交。
Git doesn't check every other branch in the repository, though; just two:
不过,Git 不会检查存储库中的所有其他分支;只有两个:
- The current branch (HEAD)
- The upstream branch, if there is one
- 当前分支(HEAD)
- 上游分支,如果有
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:
的“上游分支” experiment
,就像你的情况一样,可能是origin/experiment
. 如果experiment
在当前分支中完全合并,那么 Git 会毫无怨言地删除它。如果不是,但它已在其上游分支中完全合并,则 Git 继续发出如下警告:
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.
其中xxxxxxxx
表示提交 ID。在其上游完全合并表明提交experiment
已被推送到原始存储库,因此即使您在这里丢失它们,它们至少可以保存在其他地方。
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.
由于 Git 不检查其他分支,因此删除一个分支可能是安全的,因为您知道它已完全合并到另一个分支中;您可以使用-D
指示的选项执行此操作,或者先切换到该分支并让 Git 为您确认完全合并的状态。
回答by yongbin
You can simply figure out :
你可以简单地弄清楚:
git log --cherry master...experimental
git log --cherry master...实验
--cherry
option is a synonym for --right-only --cherry-mark --no-merges
--cherry
option 是同义词 --right-only --cherry-mark --no-merges
git-log man page said
git-log 手册页说
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.
将输出限制为我们这边的提交并使用 git log --cherry upstream...mybranch 标记那些已应用于分叉历史另一侧的提交,类似于 gitcherry 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
供参考。--cherry-pick
省略等效提交但--cherry-marks
没有。在上游和共同工作的公共分支之间找到变基并强制更新更改很有用
回答by ThorSummoner
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
.
注意:这显示了master
不在experiment
.
Don't forget to:
不要忘记:
git merge --abort
When you're done lookin.
看完后。
回答by Elta3lab
Easiest Solution With Explanation (double checked solution) (faced the problem before)
带解释的最简单解决方案(双重检查解决方案)(之前遇到过问题)
Problem is:
问题是:
1- I can't delete a branch
1-我无法删除分支
2- The terminal keep display a warning message that there are some commits that are not approved yet
2- 终端不断显示一条警告消息,指出有些提交尚未批准
3- knowing that I checked the master and branch and they are identical (up to date)
3- 知道我检查了 master 和 branch 并且它们是相同的(最新)
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,
当您的分支连接到上游远程分支(在 Github、bitbucket 或其他任何地方)时,您需要将其合并(推送)到 master,并且您需要将新更改(提交)推送到远程仓库(Github、bitbucket 或无论如何)来自分支机构,
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".
我在代码中所做的是切换到 master,然后将分支合并到其中(以确保它们在本地机器上相同),然后我再次切换到分支并将更新或更改推送到远程在线使用“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
之后又切换到master,尝试删除分支,问题(警告信息)消失,分支删除成功
回答by edW
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.
我的本地 git 上没有上游分支。我从 master 创建了一个本地分支, git checkout -b mybranch 。我在上游 git 上创建了一个带有 bitbucket GUI 的分支,并将我的本地分支 (mybranch) 推送到该上游分支。一旦我在本地 git 上执行 git fetch 以检索上游分支,我就可以执行 git branch -d mybranch。
回答by Sergey Samoylenko
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.
我相信国旗--force
是你真正要找的。只是用来git branch -d --force <branch_name>
强行删除分支。