日本語

How to Use git clone –depth to Fetch Shallow History

​​Git​CI,command line

Running git clone --depth 1 <repository> fetches only the most recent commit, without the older history. It is useful when you clone in CI (builds such as GitHub Actions) on every run, or when you want a large repository with a long history on your machine quickly, because it reduces the amount downloaded and the disk space used. A repository that holds only part of its history is called a shallow repository in Git. You can later add history with git fetch, or turn it back into a full repository. I ran the commands in a test environment and include the output from before and after each operation as-is.

Note

The test environment is Ubuntu 24.04 with Git 2.43.0. I set the author name and email to example values (Neko <neko@example.com>) and fixed the commit dates with environment variables. The source repository (neko-repo) was placed in the same test environment, and I cloned from there. I did not test how cloning from a server such as GitHub behaves, behavior on Windows and macOS, or repositories with submodules (--shallow-submodules).

The test repository and the size of a normal clone

neko-repo has five commits on the main branch. In every commit, I rewrote photo.bin, a roughly 1 MB file that does not compress, with different contents. There is also a feature branch split off from the fourth commit, and a tag v0.2 on the second commit.

console
$ git log --oneline --all --graph
* 6992511 機能を追加
| * 599aee3 バージョン5
|/  
* 8189144 バージョン4
* d16bd65 バージョン3
* d7bc963 バージョン2
* ab65ff8 バージョン1

First, I cloned without --depth. All five commits came in, and the .git folder was 5.0M. The remote branches (origin/main and origin/feature) and the tag were also all fetched.

console
$ git clone --no-local neko-repo neko-full
Cloning into 'neko-full'...

$ git -C neko-full rev-list --count HEAD
5

$ git -C neko-full branch -r
  origin/HEAD -> origin/main
  origin/feature
  origin/main

$ git -C neko-full tag
v0.2

$ du -sh neko-full/.git
5.0M	neko-full/.git

git rev-list --count HEAD prints the number of commits reachable from the current branch. I added --no-local because the source is specified as a path. The box in the next section shows what happens without it.

Fetching only the latest commit with –depth 1

I cloned with --depth 1. git log showed only one commit, and the .git folder was 1.2M. Comparing the size of the pack file stored locally with the size-pack value from git count-objects -vH, the normal clone was 4.77 MiB and the shallow clone was 978.32 KiB.

console
$ git clone --depth 1 --no-local neko-repo neko-shallow
Cloning into 'neko-shallow'...

$ cd neko-shallow

$ git log --oneline
599aee3 バージョン5

$ git rev-list --count HEAD
1

$ du -sh .git
1.2M	.git

According to the official git clone reference, --depth is an option that creates a shallow clone, cutting the history off after the specified number of commits.

Warning

If you specify the source by path (such as neko-repo or /path/to/repo), --depth has no effect unless you also pass --no-local. When I tried it without --no-local in the test environment, the following warning appeared, and all five commits were fetched. The result was not shallow. The warning suggests using a URL that starts with file:// instead. This warning does not apply when you specify a URL such as one on GitHub.

console
$ git clone -q --depth 1 neko-repo neko-local
warning: --depth is ignored in local clones; use file:// instead.

$ git -C neko-local rev-list --count HEAD
5

$ git -C neko-local rev-parse --is-shallow-repository
false

Checking whether a repository is shallow

git rev-parse --is-shallow-repository tells you whether the current repository is shallow. In %d of git log (the decoration display, which shows branch names and so on), the commit was shown as grafted.

console
$ git rev-parse --is-shallow-repository
true

$ git log --decorate=short --format="%h %d %s"
599aee3  (grafted, HEAD -> main, origin/main, origin/HEAD) バージョン5

$ cat .git/shallow
599aee359ab0042ca3293d49ec6b2b1671ab2a16

.git/shallow contains the ID of the oldest commit in the fetched range. According to Git's design notes on shallow, Git treats the commits listed in this file as commits with no parents, because they do have parents but those parents are not present locally.

History that is not present locally cannot be traversed

In a shallow repository, referring to a commit you did not fetch causes an error. When I specified the commit before the current one (HEAD~1), I got an unknown revision error.

console
$ git log --oneline HEAD~1
fatal: ambiguous argument 'HEAD~1': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

git blame also stops at the oldest commit present locally. version.txt was rewritten five times, but every line is shown as written by the バージョン5 commit, and a ^ is added at the start. git show --stat also shows photo.bin as a newly added file (Bin 0 -> 1000000 bytes).

console
$ git blame version.txt
^599aee3 (Neko 2026-11-05 10:00:00 +0900 1) v5

$ git show --stat --format="%h %s" HEAD
599aee3 バージョン5

 photo.bin   | Bin 0 -> 1000000 bytes
 version.txt |   1 +
 2 files changed, 1 insertion(+)

If you want to investigate past changes (following history with git log, finding who wrote a line with git blame, or finding the commit that caused a problem with git bisect), first add the missing history using the methods in the section "Adding or reducing history later".

With –depth, other branches and tags are not fetched

In neko-shallow, the only remote branch is origin/main, and there are no tags. The branch setting that git fetch uses (remote.origin.fetch) also covers only main.

console
$ git branch -r
  origin/HEAD -> origin/main
  origin/main

$ git tag

$ git config --get remote.origin.fetch
+refs/heads/main:refs/remotes/origin/main

This is because --depth implies --single-branch. According to the official reference, --single-branch clones the history of just one branch, and later git fetch runs update only that branch.

If you want shallow copies of other branches too, add --no-single-branch. Each branch then gets only its latest commit, and the commit tagged v0.2 was also fetched.

console
$ git clone -q --depth 1 --no-single-branch --no-local neko-repo neko-all

$ git -C neko-all branch -r
  origin/HEAD -> origin/main
  origin/feature
  origin/main

$ git -C neko-all log --oneline --all
6992511 機能を追加
599aee3 バージョン5
d7bc963 バージョン2

$ git -C neko-all tag
v0.2

If you need only one specific branch, not main, specify the branch name with --branch.

console
$ git clone -q --depth 1 --branch feature --no-local neko-repo neko-feature

$ git -C neko-feature log --oneline
6992511 機能を追加

$ git -C neko-feature branch -r
  origin/feature

Adding or reducing history later

From here on, I worked with neko-shallow, which was cloned with --depth 1. Options passed to git fetch change how much history is kept locally (official git fetch reference).

--deepen=2 fetches two more commits back from the current oldest local commit. The ID in .git/shallow also changed to the new oldest commit, バージョン3.

console
$ git fetch -q --deepen=2

$ git log --oneline
599aee3 バージョン5
8189144 バージョン4
d16bd65 バージョン3

$ cat .git/shallow
d16bd65055e6c939231a960d0e4675610d8baae5

--depth redefines the local history length as a count of commits from the tip of the branch. Specifying a smaller number than the current one (here, 1) made the local history shorter.

console
$ git fetch -q --depth=1

$ git log --oneline
599aee3 バージョン5

--shallow-since sets the range by date and time instead of by count. Commits after midnight on November 3 (Japan time) were fetched. Here I specified the time and time zone in full.

console
$ git fetch -q --shallow-since="2026-11-03 00:00 +0900"

$ git log --oneline
599aee3 バージョン5
8189144 バージョン4
d16bd65 バージョン3

Restoring full history with –unshallow

Running git fetch --unshallow fetched all the remaining history, and the repository was no longer shallow. .git/shallow was also deleted.

console
$ git fetch -q --unshallow

$ git log --oneline
599aee3 バージョン5
8189144 バージョン4
d16bd65 バージョン3
d7bc963 バージョン2
ab65ff8 バージョン1

$ git rev-parse --is-shallow-repository
false

$ ls .git/shallow
ls: cannot access '.git/shallow': No such file or directory

However, the branch setting still covers only main, so the feature branch was not fetched. After I changed the setting to fetch all branches ("*") with git remote set-branches and ran git fetch, origin/feature appeared.

console
$ git branch -r
  origin/HEAD -> origin/main
  origin/main

$ git config --get remote.origin.fetch
+refs/heads/main:refs/remotes/origin/main

$ git remote set-branches origin "*"

$ git fetch -q && git branch -r
  origin/HEAD -> origin/main
  origin/feature
  origin/main

Shallow clones can be in use in CI

According to the README of actions/checkout, which GitHub Actions uses to clone, by default it fetches only the single commit that triggered the workflow. The README says that if you need the full history, all branches, and tags, you should specify fetch-depth: 0. When a CI step runs a command that uses past history, you can run git rev-parse --is-shallow-repository to check whether the clone is shallow.

​Posted by 86