git 如何处理合并中的提交?

发布于 2024-11-16 04:05:40 字数 2537 浏览 2 评论 0原文

我无法理解 git merge 如何在合并过程创建的提交方面工作。我已经阅读了 pro git 和 git 社区书籍中的相关部分,但仍然感到困惑。

考虑这种情况:我有一个“原始”git 存储库:

          master
            |
a0--a1--a2--a3
  \
   -b0--b1
         |
      branch2

我将此存储库克隆到本地存储库,然后仅在本地存储库上工作。在branch2中,我做了一个“git merge master”。现在我的本地存储库看起来像这样:

     master / origin/master
            |
a0--a1--a2--a3
  \           \
   -b0--b1-----merge
         |       |
  origin/branch2 |    
              branch2

因此,合并在“b1”提交之后创建了 1 个提交“merge”。 branch2 的“git log”显示了预期的图表:

> git log branch2 --graph --pretty=oneline --decorate
*   a7b69fc6759e1dd5463bab379ce101a6ad026c7b (HEAD, branch2) Merge branch 'master' into branch2
|\  
| * 482e4026f05e33395a9fc3c87f50a746f692406a (origin/master, origin/HEAD, master) a3
| * 8de57bdea2d316073af3b7055f8c28c56004ce94 a2
| * 1e991047996aad269f2c01b9a0b9a7d8293096a1 a1
* | 99955f66843df51fb9d40c7797156c32cad57415 (origin/branch2) b1
* | 30ca9b6363558322f2bb7810d75cda0d9c2ba3e0 b0
|/  
* 76a7c6d0eb54a9580841115ff2c3429360ab2ac9 a0

此外,如果我在当前头之前进行一次提交,我会按照预期到达 b1 提交。 (沿着图中的branch2线并返回1次提交)

> git log branch2~ --graph --pretty=oneline --decorate
* 99955f66843df51fb9d40c7797156c32cad57415 (origin/branch2) b1
* 30ca9b6363558322f2bb7810d75cda0d9c2ba3e0 b0
* 76a7c6d0eb54a9580841115ff2c3429360ab2ac9 a0

我的困惑来了。我还没有将我的更改推送到原点,但是当我执行“git status”时,git 说我的本地branch2 领先于origin/branch2 4 个提交。我认为合并仅导致 1 个新的“合并”提交,如上图/图表所示?为什么是4?

> git status
# On branch branch2
# Your branch is ahead of 'origin/branch2' by 4 commits.
#
nothing to commit (working directory clean)

另外,从origin/branch2到branch2执行“git log”显示了4个提交,而不是我预期的1个(从“b1”到“merge”)。

> git log origin/branch2..branch2 --graph --pretty=oneline --decorate
* a7b69fc6759e1dd5463bab379ce101a6ad026c7b (HEAD, branch2) Merge branch 'master' into branch2
* 482e4026f05e33395a9fc3c87f50a746f692406a (origin/master, origin/HEAD, master) a3
* 8de57bdea2d316073af3b7055f8c28c56004ce94 a2
* 1e991047996aad269f2c01b9a0b9a7d8293096a1 a1

我知道这 4 个提交是来自 master (a1,a2,a3) 的 3 个提交,合并应该将其带到分支 2,加上“合并”提交。但为什么图表不这样显示呢?如果是这样,它看起来像:

     master / origin/master
            |
a0--a1--a2--a3-----------
  \                      \
   -b0--b1--a1'--a2'--a3'--merge
         |                  |
  origin/branch2            |    
                         branch2

并且branch2〜会将我带到a3'而不是b1。

预先感谢您的任何帮助。

I'm having trouble understanding how a git merge works in terms of the commits created by the merge process. I've read the relevant sections in pro git and the git community book, but am still confused.

Consider this scenario: I have an "origin" git repo:

          master
            |
a0--a1--a2--a3
  \
   -b0--b1
         |
      branch2

I clone this repo to a local repo and then work only on the local repo. In branch2, I did a "git merge master". Now my local repo looks like this:

     master / origin/master
            |
a0--a1--a2--a3
  \           \
   -b0--b1-----merge
         |       |
  origin/branch2 |    
              branch2

So the merge created 1 commit, "merge", after the "b1" commit. "git log" for branch2 shows the expected graph:

> git log branch2 --graph --pretty=oneline --decorate
*   a7b69fc6759e1dd5463bab379ce101a6ad026c7b (HEAD, branch2) Merge branch 'master' into branch2
|\  
| * 482e4026f05e33395a9fc3c87f50a746f692406a (origin/master, origin/HEAD, master) a3
| * 8de57bdea2d316073af3b7055f8c28c56004ce94 a2
| * 1e991047996aad269f2c01b9a0b9a7d8293096a1 a1
* | 99955f66843df51fb9d40c7797156c32cad57415 (origin/branch2) b1
* | 30ca9b6363558322f2bb7810d75cda0d9c2ba3e0 b0
|/  
* 76a7c6d0eb54a9580841115ff2c3429360ab2ac9 a0

In addition, if I go one commit before the current head, I get to the b1 commit, as expected. (follow the branch2 line in the graph and go back 1 commit)

> git log branch2~ --graph --pretty=oneline --decorate
* 99955f66843df51fb9d40c7797156c32cad57415 (origin/branch2) b1
* 30ca9b6363558322f2bb7810d75cda0d9c2ba3e0 b0
* 76a7c6d0eb54a9580841115ff2c3429360ab2ac9 a0

Here comes my confusion. I haven't pushed my changes to the origin yet, but when I do a "git status", git says my local branch2 is ahead of the origin/branch2 by 4 commits. I thought the merge only resulted in 1 new "merge" commit, as seen by the graph / diagrams above? Why 4?

> git status
# On branch branch2
# Your branch is ahead of 'origin/branch2' by 4 commits.
#
nothing to commit (working directory clean)

Also, doing a "git log" from origin/branch2 to branch2 shows the 4 commits instead of the 1 I expected (going from "b1" to "merge").

> git log origin/branch2..branch2 --graph --pretty=oneline --decorate
* a7b69fc6759e1dd5463bab379ce101a6ad026c7b (HEAD, branch2) Merge branch 'master' into branch2
* 482e4026f05e33395a9fc3c87f50a746f692406a (origin/master, origin/HEAD, master) a3
* 8de57bdea2d316073af3b7055f8c28c56004ce94 a2
* 1e991047996aad269f2c01b9a0b9a7d8293096a1 a1

I understand the 4 commits are the 3 from master (a1,a2,a3) that the merge is supposed to bring over to branch 2, plus the "merge" commit. But why doesn't the graph show it that way? If it does, it would look like:

     master / origin/master
            |
a0--a1--a2--a3-----------
  \                      \
   -b0--b1--a1'--a2'--a3'--merge
         |                  |
  origin/branch2            |    
                         branch2

And branch2~ would bring me to a3' instead of b1.

Thanks in advance for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

锦上情书 2024-11-23 04:05:40

正如“如何获取git 分支上的更改”:

git log origin/branch2..branch2

表示:branch2 上的所有提交(正引用)不在 origin/branch2 上(负引用)参考)
(关于正面和负面参考,请参阅“'git log origin/master' 与 'git log origin/master..' 的区别")

在您的情况下,合并后,您在 branch2(本地)上确实有 4 个提交,但不在 origin/branch2 上:a1a2a3合并

As mentioned in "How to get the changes on a branch in git":

git log origin/branch2..branch2

means: all the commits on branch2 (positive reference) which aren't on origin/branch2 (negative reference)
(On the positive and negative references, see "Difference in 'git log origin/master' vs 'git log origin/master..'")

In your case, after the merge, you do have 4 commits on branch2 (the local one) that aren't on origin/branch2: a1, a2, a3 and merge.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文