Bai Bing
Bai Bing

Reputation: 380

Git Master Branch Containing other branch info

This might be a default behavior but it's giving me hard time to understand. I always thought a branch must be one line, without other "branches". I made the following experiment:

  1. Init a git repo.
  2. Commit twice(m1,m2) on master branch
  3. Checkout a new branch named "dev"
  4. Commit twice(d1,d2) on dev branch.
  5. Checkout master
  6. Commit twice(m3,m4)

Now the branch looks like below

Before Merge

From my understanding, there are 4 checkins in master branch, 2 checkins in dev branch.

Now I perform the following operations:

  1. Merge dev branch (git merge dev)

The branch looks like below:

enter image description here

I would think that the master branch is the green one, it contains 5 commits. The dev branch is the purple one, it contains 4 commits.

Actually, in master branch, I use git log to check the commits, it actually has 7 commits. Which confuses me.

Can someone help explain?

Upvotes: 0

Views: 37

Answers (1)

Romain Valeri
Romain Valeri

Reputation: 21918

git log lists all commits reachable from the given ref (or HEAD if no refs are given). The 7 commits you see are reachable. You might need to give log some other specific parameters if you want to see only parts of the tree. (Check the doc there are great examples)

Also, your surprise can be a consequence of a misunderstanding about branches in git. No commit in the tree belongs to any branch. The whole tree can be accessed through different points, and branches are just shortcuts to point at one commit.

Upvotes: 1

Related Questions