How to shallow clone a specific commit with depth 1?

GitShallow Clone

Git Problem Overview


Is it possible to shallow clone a specific commit in a repository, i.e. with depth 1? Something like

git clone http://myrepo.git 728a4d --depth 1

to get the repository state as it is at the commit with SHA 728a4d...?

The motivation is to avoid having to clone the whole repository, then check out that specific commit, when we're only interested in the state of the repository at that specific commit.

Git Solutions


Solution 1 - Git

Starting with Git 2.5.0 (which needs to be available at both the client and server side) you can set uploadpack.allowReachableSHA1InWant=true on the server side to enable fetching of specific SHA1s:

git init
git remote add origin <url>
git fetch --depth 1 origin <sha1>
git checkout FETCH_HEAD

Note that I did not find a syntax to do this with git clone directly.

Solution 2 - Git

NOTE: My example doesn't help to clone to by a commit hash but it will help to clone a tag and have a lightweight repository.

If you have to have only one commit in your "clone" and you are going to use commit hash, short answer is NO.

I use this command construction (tested on v2.13.2.windows.1) for tags:

git clone --depth 1 git@github.com:VENDOR/REPO.git --branch 1.23.0 --single-branch

Full example:

$ git clone --depth 1 [email protected]:Seldaek/monolog.git --branch 1.23.0 --single-branch
Cloning into 'monolog'...
remote: Counting objects: 201, done.
remote: Compressing objects: 100% (188/188), done.
remote: Total 201 (delta 42), reused 32 (delta 5), pack-reused 0
Receiving objects: 100% (201/201), 190.30 KiB | 0 bytes/s, done.
Resolving deltas: 100% (42/42), done.
Note: checking out 'fd8c787753b3a2ad11bc60c063cff1358a32a3b4'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

$ cd monolog

.git dir size (267K vs 2.6M by using full clone):

$ du -h --max-depth=0 .git
267K    .git

I'd like to denote, --branch can take a tag/branch.

https://git-scm.com/docs/git-clone#git-clone---branchltnamegt > --branch can also take tags and detaches the HEAD at that commit in the resulting repository.

UPD

In a nutshell, it can take "refs". You may read more here: What does the git error message “Server does not allow request for unadvertised object” mean?

Also, there is no tricks like:

git fetch --depth 1 origin <COMMIT_HASH>

Thanks @BenjiWiebe for pointing me in my mistake.

Solution 3 - Git

Try using while in bash:

git clone --depth=1 $url
i=1; while ! git show $sha1; do git fetch --depth=$((i+=1)); done

This is pretty slow because it fetches each commit individually; you could increase the increment (to fetch commits in batches and improve performance over a network) but it's still a brute force approach.

Solution 4 - Git

The immediate answer is: You can't do it using a git clone directly.
Why? A detailed explanation can be found here: https://stackoverflow.com/questions/26135216/why-isnt-there-a-git-clone-specific-commit-option

What else can you do?

How to clone the repository to a specific commit? (full clone)
# Create empty repository to store your content
git clone <url>
git reset <sha-1> --hard

More info:

How to clone a single branch?

git clone <url> --branch <branch_name> --single-branch <folder_name>

How to clone only latest commit from a given branch?

git clone <url> --depth=1 --branch <branch_name> --single-branch <folder_name>

How to shallow clone a specific commit with depth 1?

As @sschuberth commented out: --depth implies --single-branch.

Instead of clone use the fetch command:

# fetch a commit (or branch or tag) of interest
# In this case you will have the full history of this commit
git fetch origin <sha1>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChinView Question on Stackoverflow
Solution 1 - GitsschuberthView Answer on Stackoverflow
Solution 2 - GitKirbyView Answer on Stackoverflow
Solution 3 - GitCorinView Answer on Stackoverflow
Solution 4 - GitCodeWizardView Answer on Stackoverflow