How to Check if a Git Repository is Shallow?

Starting with Git v2.15, you can use the following command in the root directory of a repository to check if a Git repository is a shallow clone or not:

# Git 2.15+
$ git rev-parse --is-shallow-repository

For versions of Git prior to 2.15, you can simply check to see if the ".git/shallow" file exists in the Git repository directory, for example, using the following command:

$ ([ -f "$(git rev-parse --git-dir)"/shallow ] && echo true) || echo false

This command is merely a shorter way of writing the following:

if [ -f "$(git rev-parse --git-dir)"/shallow ]; then
  echo true
else
  echo false
fi

Running any of these commands will output one of the following:

  • "true" — this means that the repository is a shallow clone — i.e. it contains only a specific number of recent commits instead of the entire commit history;
  • "false" — this means that repository is not a shallow clone, and contains the complete commit history.

This post was published by Daniyal Hamid. Daniyal currently works as the Head of Engineering in Germany and has 20+ years of experience in software engineering, design and marketing. Please show your love and support by sharing this post.