为什么 git 抱怨某些看起来已合并但未合并的内容?

Why does git complain something isn't merged when it looks like it is merged?

我在 git 中创建了一个 b运行ch 来处理我的功能(b运行ch 称为 my_branch)。我将更改推送到 b运行ch,然后将 b运行ch 重新定位到 master。我的公司使用 git rebase 而不是 git merge。

我运行以下命令:

git checkout master
git rebase master my_branch
git checkout master     (seems like I somehow got switched to my_branch so had to switch back to master)
git merge --ff-only my_branch
git log                 (made sure my code is now in master)
git push origin master

当我尝试清理和删除我的本地 b运行ch 时,它发出警告,提示它尚未合并到 refs/remote/origin/my_branch。

# git branch -d my_branch
warning: not deleting branch 'my_branch' that is not yet merged to
         'refs/remotes/origin/my_branch', even though it is merged to HEAD.
error: The branch 'my_branch' is not fully merged.
If you are sure you want to delete it, run 'git branch -D my_branch'.

(如有错别字,请原谅上述消息)

当我通过浏览器查看我们的 git 存储库并查看 b运行 目录时,我可以在“my_branch”中看到我最新的 code/check-in。我也可以在 master b运行ch 中看到我的代码。

为什么 git 抱怨“my_branch”没有完全合并?我可以安全地删除我的本地 b运行ch 还是 git 指向“my_branch”中的更改,一旦我在本地删除它就会消失?如果我也远程删除 b运行ch,会不会有什么影响?

您在此过程中遗漏了一个步骤:在您变基主题分支 (my_branch) 之后,您需要 force-push 它以在您的远程更新它。

  1. git结帐大师
  2. git 变基大师 my_branch
    • 检查 my_branch 以执行变基是预期的行为
  3. git推--force-with-lease来源my_branch
    • rebasing改写了my_branch的历史,所以推送需要强制推送
    • 我编辑了我的答案,使其成为 --force-with-lease 而不仅仅是 --force
  4. git结帐大师
  5. git合并--ff-only
  6. git push origin master

Git 抱怨 my_branch 与其远程分支(origin/my_branch)“未完全合并”,因为本地和远程分支在历史上已经分道扬镳。它不是抱怨它与 master 完全合并。

综上所述 - 即使有警告,您实际上也可以删除分支。它不会有任何负面影响。 Git 只是警告您,您正在删除与其远程不同步的分支。