xged xged - 2 months ago 22x
Git Question

Why git shallow clone can have more commits than <depth>?

git clone --depth <depth> <url> <repo>; git -C <repo> rev-list --count --all
!= depth

git clone --depth <depth>
: Create a clone with a history truncated to
commits. Implies

E.g.: url = https://github.com/vhf/free-programming-books.git, depth = 10, then commit_count = 15

git version


The shallow version of the repository includes all commits within the specified distance from the branch head when following all possible parents rather than only the first one. Therefore, for a non-linear history with merges, the count of commits will not be equal to depth.

$ git clone --depth 10 https://github.com/vhf/free-programming-books.git
Cloning into 'free-programming-books'...
remote: Counting objects: 85, done.
remote: Compressing objects: 100% (63/63), done.
remote: Total 85 (delta 31), reused 46 (delta 22), pack-reused 0
Unpacking objects: 100% (85/85), done.
Checking connectivity... done.

$ git -C free-programming-books/ rev-list --count HEAD

$ git -C free-programming-books/ log --graph --oneline --decorate
* b9ffc8e (HEAD -> master, origin/master, origin/HEAD) Adding pt_BR C book used by ...
* 824c1d3 Replaced Google Python style guide dead-link with new one (#1987)
* 3c32612 Added Laravel: Code Smart online book (#1986)
* eabce2c Fixed typo: Structure and Interpretation (#1985)
* aab83e5 Added IRPF90 gitbook to Misc section (#1984)
* 6f72509 Added a bash tutorial in free-courses-en.md (#1983)
*   9b95b09 Merge branch 'pr/1980'
| * 2811cd3 Fix blank lines
| * bbe9bd6 Adds 2 golang podcasts (and fixes missing #ggulp)
| * fdeabc6 (grafted) Fix ordering
*   da317ad Merge branch 'pr/1976'
| * 20b940a Fix ordering
| * 9a6ee0b (grafted) Add openHPI to list of MOOCs
* 43294d1 Update link Rust by Example #1970 (#1995)
* d758a93 (grafted) Fix a broken link to 'Practical PostgreSQL' (#1994)

The pseudo-root commits in this truncated history are d758a93, 9a6ee0b, and fdeabc6. All of them are 9th generation ancestors of the head commit.

Original answer (before the MVCE was provided)

This may be the case with local clones when <url> doesn't start with file://. Then git outputs a corresponding warning:

warning: --depth is ignored in local clones; use file:// instead.

For local clones git simply creates hardlinks to objects in the source repository (unless the --no-hardlinks switch is specified, but the latter still doesn't make local clones respect the --depth option). Cloning via hardlinking allows to save disk-space and is very fast compared to copying all objects. Since the purpose of the --depth option is to reduce the data transfer, it doesn't make much sense for local clones and is therefore ignored.