How does git handle merge in merge?

It's hard for me to understand how git merging works in terms of the commits created by the merge process. I read the relevant sections in pro git and the git community, but I'm still confused.

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

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 performed the "git merge wizard". 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 committing "b1". The "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 

Also, if I switch to one commit before the current chapter, I get the message b1, as expected. (follow the line branch2 on the chart and return 1 commit)

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

Here comes my confusion. I haven't changed my changes at the origin yet, but when I execute git status, git says that my local branch2 is 4 commits ahead of start / branch2. I thought the merger only led to 1 new β€œmerge”, as can be seen from 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) 

In addition, executing a β€œgit log” from origin / branch2 to branch2 shows 4 commits instead of the expected 1 (transition 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 that 4 commits are 3 from master (a1, a2, a3), which are supposed to merge into branch 2, plus a merge commit. But why does the chart not show this? If this happens, it will look like this:

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

And branch2 ~ will lead me to a3 'instead of b1.

Thanks in advance for your help.

+6
source share
1 answer

As stated in " How to get changes to a branch in git ":

 git log origin/branch2..branch2 

means: all commits on branch2 (positive link) that are not on origin/branch2 (negative link)
(For positive and negative links, see " Difference in ' git log origin/master ' vs ' git log origin/master.. ' ")

In your case, after the merge, you have 4 commits on branch2 (local) that are not on origin/branch2 : a1 , a2 , a3 and merge .

+6
source

Source: https://habr.com/ru/post/890795/


All Articles