两个分支如何在git中“链接”?

3
我最近遇到了一件非常奇怪的事情,我无法解释,这几乎肯定是因为我对 git 和 GitHub 的理解不足。 我正在使用 GitHub 流程 模型在 GitHub 上开展一个项目。有两个功能分支正在积极地进行开发-我们称之为 A 和 B。它们都在快乐地进行自己的提交,直到 A 分支上出现了一个提交,内容如下:

Merge remote-tracking branch 'origin/b into b'

从那时起,A 分支的拉取请求显示了 A 和 B 两个分支的提交记录。我没有注意到这一点,在将 A 分支合并到主分支时,它同时关闭了 GitHub 中 B 分支的拉取请求。在关闭 B 分支的拉取请求后回顾它,它似乎“丢失”了除两个以外的所有提交。这是怎么发生的?有什么 git 命令可以链接分支(和 PR)以这种方式吗?我想能够在干净的代码库中重现这个问题,并作为团队学习的示例。

2
您可能曾经在过去的某个时候,在分支 A 上执行了 git pull origin B - Vishwanath
1个回答

0
分支B的提交如何进入分支A? 看起来您将分支B的更改拉取或合并到了分支A中,然后将包含A和B提交的分支A推送到了GitHub。以下是一个最小可行示例:
git clone <your repo clone url>; cd <reponame>
git commit --allow-empty -m "first commit guarantees shared history"
git checkout -b B
touch bar; git add bar; git commit -m "commit on branch B"
git push origin B
git checkout master
git checkout -b A
touch foo; git add foo; git commit -m "Commit on branch A"
git push origin A
# Create PR from A into master using GitHub UI
git status
On branch A
nothing to commit, working directory clean
git merge B
# your editor opens and you make a commit
git push origin A

现在,当您键入git merge B时,在分支B上的所有提交都会被合并到分支A中。当您将它们推送到分支A时,它们将出现在从A到主分支的拉取请求中,因为GitHub假定对分支的后续提交是为了解决关于拉取请求的反馈。

为什么从B到主分支的PR被关闭了?

虽然您可能已经习惯了通过UI中的按钮合并PR,但在命令行上执行合并会产生完全相同的效果。GitHub决定是否手动合并PR的方法是将PR中提交的ID与您创建该PR的分支(在本例中为master)中提交ID进行比较。由于从分支B中的所有提交已作为分支A的一部分合并到主分支中,因此对GitHub而言,这看起来与手动合并分支B完全相同,因此PR被标记为"已合并",不再显示为"打开"。

哪些git命令可以"链接"分支/ PR?

  • git merge otherbranch; git push remotename PRbranch
  • git pull remotename branchname; git push remotename PRbranch
  • git cherry-pick <commit range>; git push remotename PRbranch if you cherry-pick all the commits from some other PR
  • git apply <patchfile>; git push remotename PRbranch if for some reason the entire contents of another PR had been emailed to you as a patch

基本上,任何将来自另一个分支的提交带入您的历史记录的命令都有可能导致您看到的行为。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接