下面是我从主分支中使用的命令

git branch experiment
git checkout experiment

然后我对我的文件做了一些更改,提交这些更改,并将新的分支推送到GitHub。

git commit . -m 'changed files'
git push -u origin experiment

后来,我决定将我的实验分支合并到主分支中。

git checkout master
git merge experiment

最后,我把这些改动推到了GitHub上。

git push -u origin master

一切都很顺利,直到我试图删除我的实验分支使用

git branch -d experiment

我得到了错误信息:

错误:分支'experiment'没有完全合并。 如果你确定要删除它,运行'git branch -D experiment'。

我对git有点陌生,我不知道我还能合并多少两个分支。我错过了什么?


当前回答

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'.

对我来说,解决方案很简单,就是需要将功能分支推到远程。当我跑的时候:

git push origin guided-furniture
/* You might need to fetch here */
git branch -d guided-furniture

Deleted branch guided-furniture (was 1813496).

其他回答

我今天就遇到了这种情况,因为我正在将我的第一个功能分支合并回master中。正如一些人在SO的其他地方说的,诀窍是在尝试删除分支之前切换回master。一旦回到master, git很乐意在没有任何警告的情况下删除分支。

有解释的最简单解决方案(重复检查解决方案)(之前遇到过这个问题)

问题是:

1-我不能删除分支

2-终端保持显示一个警告消息,有一些提交还没有被批准

3-知道我检查了主和分支,他们是相同的(最新)

解决方案:

git checkout master
git merge branch_name
git checkout branch_name
git push
git checkout master
git branch -d branch_name

解释:

当你的分支连接到上游的远程分支(在Github, bitbucket或其他平台上)时,你需要将它合并(推送)到主平台,你需要将新的更改(提交)从分支推送到远程repo (Github, bitbucket或其他平台),

我在我的代码中所做的是,我切换到master,然后将分支合并到它(以确保它们在你的本地机器上是相同的),然后我再次切换到分支,并使用“git push”将更新或更改推到远程在线回购。

之后,我再次切换到master,尝试删除分支,问题(警告消息)消失,分支删除成功

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'.

对我来说,解决方案很简单,就是需要将功能分支推到远程。当我跑的时候:

git push origin guided-furniture
/* You might need to fetch here */
git branch -d guided-furniture

Deleted branch guided-furniture (was 1813496).

公认的答案是正确的。在这里,我想补充三点:

这是如何发生的(经常发生在我身上) 举个例子 如何确保在通过-D强制删除之前不遗漏任何更改

我在GitHub上使用Rebase和merge作为默认的PR合并方法。这将为相同的更改创建新的提交(哈希)。

例如,当我跑步的时候

git log --graph --left-right --oneline add-theme-dark-2...main

在我的一个项目中,我得到了这个:

> 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)

注意1a/1b和2a/2b有不同的提交哈希值。

要确保您没有错过更改,请运行:

git log --graph --left-right --cherry-pick --oneline add-theme-dark-2...main

如果它返回一个列表,则每一行都以“=”开头:

= 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.

使用以下命令可以安全地删除分支:

git branch -D add-theme-dark2

我的本地git上没有上游分支。我已经从master创建了一个本地分支,git checkout -b mybranch。我在上游git上用bitbucket GUI创建了一个分支,并将我的本地分支(mybranch)推到该上游分支。一旦我在我的本地git上进行了git取回来检索上游分支,我可以做一个git分支-d mybranch。