gsf gsf - 1 year ago 101
Git Question

Get git sha depth on a remote branch

I would like to use

git clone --depth [N]
, but to use such
that guarantees that a particular sha is obtained.

How I can determine the depth of a sha of in a remote repo. Note cloning it locally to do that is catch 22. I would like to do that to avoid cloning it all.

Answer Source

Option 1:

If you have the ability to get to a full clone of the repository, you can find the depth using git rev-list HEAD ^42c6ee9 --count.

This will find the depth of any particular commit. There is no remote version so this only works if you can maintain a full copy and then ssh into it to figure out the depth.

This allows you to only have to clone once but then you'll be able to answer the question for all the following times you'd like to do a shallow copy.

Option 2:

Using git clone --depth 1 then iterating on git fetch --depth=i+1 is actually a good idea worth testing. (Also proposed by @leon above).

Depending on the characteristics of your repository, this will make sense.

E.g. Django repository has 23330 commits (at the time of testing)

./ - pulling the full local repository

git clone

./ - if you magically knew the right answer. lower bound on time.

git clone --depth 10

./ - iterating

git clone --depth 1
cd django

until git show 5d35181 > /dev/null
    git fetch --depth=$i

The search, while there is overhead, may still come out faster then a full clone.

./  21.34s
./ 1.12s
./ 3.05s
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download