August Feng

git ordering

Commit history

Let's say there is a commit history that is described by this graph, where x is older than b.

  v
  | \
  |  |
  c  |
  |  |
  b  |
  |  z
  |  |
  |  y
  |  |
  |  x
  | /
  a

git log

If we graph it then x will appear more recent.

git log --oneline --graph
# *   f9df10e Merge branch 'foobar'
# |\  
# | * 47992e4 z
# | * 4ebdf15 y
# | * 727dff6 x
# * | efb81f9 c
# * | 816ffa8 b
# |/  
# * ebd0cd6 a

If we however ask for it without graphing, then it's sorted chronologically.

git log --oneline
# f9df10e Merge branch 'foobar'
# efb81f9 c
# 816ffa8 b
# 47992e4 z
# 4ebdf15 y
# 727dff6 x
# ebd0cd6 a

git fetch –depth=n

If we init another git repository and fetch with depth 1, we'll only the merge commit as expected.

  git fetch --depth 1
  git checkout main
  git log
  # f9df10e Merge branch 'foobar'

What's interesting is when we fetch with depth 2, the foobar branch will have two commits fetched (z and y) whereas the main branch only has c that is fetched.

  git log --oneline --graph
  # *   f9df10e Merge branch 'foobar'
  # |\  
  # | * 47992e4 z
  # | * 4ebdf15 y
  # * efb81f9 c

This is because the merge commit belongs to the main branch, so it's considered part of the depth. To confirm this, we let's increase the fetch depth to 3:

  git log --oneline --graph
  # *   f9df10e Merge branch 'foobar'
  # |\  
  # | * 47992e4 z
  # | * 4ebdf15 y
  # | * 727dff6 x
  # * efb81f9 c
  # * 816ffa8 b

git fetch –deepen=n

Earlier we learned that a fetch with depth 2 will grab two commits from the branch that is merged in and will only get one additional commit besides the merge commit for the main branch.

We can use git fetch --deepen=1 to symetrically increase the depth:

  git fetch --depth=1
  git fetch --deepen=1

Disclaimer: this just did the same thing locally, but it did work as expected for github actions:

  Run git log --oneline --graph
  # *   2c0a785 Merge 19fd64007f39436152c677ce7e2bf0bac8bdd7ea into 5e6ed9a9caa59f2c33ac9f8e47875ef67ebe507a
  # |\  
  # | * 19fd640 <commit in branch>
  # * 5e6ed9a <commit in main>