How to Test if Git Repository is Shallow?

If your Git is 2.15 or later, run:

git rev-parse --is-shallow-repository

which will print false (not shallow) or true (shallow):

if $(git rev-parse --is-shallow-repository); then
    ... repository is shallow ...
fi

The answer below dates back to Git versions before 2.15.


If your Git is older than 2.15,1 just test for the file shallow in the Git repository directory:2

if [ -f "$(git rev-parse --git-dir)"/shallow ]; then
    echo this is a shallow repository;
else
    echo not a shallow repository;
fi

or (shorter):

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

You can turn this into a shell function:

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

and even automate the Git version checking:

test_shallow() {
    set -- $(git rev-parse --is-shallow-repository)
    if [ x$1 == x--is-shallow-repository ]; then
        [ -f "$(git rev-parse --git-dir)"/shallow ] && set true || set false
    fi
    echo $1
}

1git --version will print the current version number:

$ git --version
2.14.1

$ git --version
git version 2.7.4

etc. (I have multiple versions on different VMs/machines at this point.) You can also run:

git rev-parse --is-shallow-repository

If it just prints --is-shallow-repository, your Git is pre-2.15 and lacks the option.

2To see why there are double quotes around $(git rev-parse --git-dir), see Tom Hale’s comment. Note that testing this is a bit tricky since git rev-parse --git-dir from the top level of, e.g., the repository /tmp/with space just prints .git; you must be in a subdirectory, such as /tmp/with space/sub to observe the problem.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)