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.
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:
1.23.0
- it can be either a commit hash or a branch.Full example:
.git
dir size (267K vs 2.6M by using fullclone
):I'd like to denote,
--branch
can take a tag/branch.https://git-scm.com/docs/git-clone#git-clone---branchltnamegt
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:
Thanks @BenjiWiebe for pointing me in my mistake.
Try using
while
in bash: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.
The immediate answer is: You cant.
Why? detailed explain can be found here: Why Isn't There A Git Clone Specific Commit Option?
What else can you do?
How to clone repository to a specific commit? (full clone)
More info:
How to clone 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:
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:Note that I did not find a syntax to do this with
git clone
directly.