Search code examples
gitsparse-checkoutshallow-clone

Creating a sparse and shallow clone of a git repository results in error: trying to write non-commit object


I want to create a sparse and shallow clone of a git repository at a certain tag.

This is what I am currently doing:

git init avatica-tmp

cd avatica-tmp

git remote add origin https://github.com/apache/calcite-avatica/

git config core.sparsecheckout true

echo "core/src/main/protobuf/*" >> .git/info/sparse-checkout

git pull --depth=1 origin rel/avatica-1.10.0

It works correctly, but throws an error:

remote: Counting objects: 531, done.
remote: Compressing objects: 100% (381/381), done.
remote: Total 531 (delta 147), reused 280 (delta 51), pack-reused 0
Receiving objects: 100% (531/531), 963.03 KiB | 233.00 KiB/s, done.
Resolving deltas: 100% (147/147), done.
From https://github.com/apache/calcite-avatica
 * tag               rel/avatica-1.10.0 -> FETCH_HEAD
fatal: update_ref failed for ref 'HEAD': cannot update ref 'refs/heads/master': trying to write non-commit object fe4f0b4ea3e2ee4f3b2e82329363a7945493a8c9 to branch 'refs/heads/master'

I get this error with Git 2.11.0 on Ubuntu 17.04 and Git 2.13.2.windows.1 on Windows 10 64-bit. I am not attempting to clone to a mapped drive.

What causes this error and how do I avoid it?


Solution

  • You are trying to pull an annotated tag into your current branch with a shallow clone in an empty repository and I guess it's the problem as it's trying to set your local master branch to point to the tag and git doesn't like it.

    A solution is to use fetch then checkout the fetched tag:

    git fetch --depth=1 origin rel/avatica-1.10.0
    git checkout FETCH_HEAD