This article discusses the various methods we can employ to clone a Git repository with a specific revision or change set. It is convenient when you have a repository with large files and only need a specific code version.
Instead of cloning the whole repository, you can get a specific change set that can help reduce the clone time, especially if you are dealing with large files.
Clone a Git Repository With a Specific Revision
We can use several methods to clone a repository with a specific change set. Let’s start with the recent development in Git that allows us to use the
git fetch command with a commit of interest.
Before Git Version 2.5.0, the
git fetch origin <commit id> did not work. With the release of version 2.5.0, Git came included with the option of configuring the
true on the server side.
Once set to
true, we can fetch any reachable commit. This feature is off by default and is discouraged for security and performance reasons.
Some Git servers like Bitbucket usually have this feature on by default. Follow these steps to fetch small subsets of a large Git repo at specific change sets.
We initialize a Git repository in the directory we wish to clone to.
$ git init
We will then add our remote to the local repo.
$ git remote add origin <url://to/source/repository
We can now fetch the commit we want, as illustrated below.
$ git fetch origin <sha1-of-commit-of-interest>
Finish the process by resetting the branch to the commit of interest.
$ git reset --hard FETCH_HEAD
Note that this will fetch the full commit history, including the specified commit. You can limit this with the
--depth=...' or '--shallow-since=... flags.
This method is not safe and may not work for all Git servers. Are there any alternatives for this method that arrive at the same destination?
The method we are about to discuss is ideal for small repositories. It will still work with large repositories but will require patience from your side.
Assuming you already know which commit you want to clone from, start by cloning the entire repository.
Start by adding the remote to your local repo and proceed to clone, as shown below.
$ git clone <repo url>
Once done, move
HEAD to the commit of interest using the
git reset command, as illustrated below.
$ git reset --hard <commit id>
If you want to return to the most recent commit, run the
git pull command.
Alternatively, you can clone the repository and switch to the commit of interest using the
git checkout command, as shown below.
$ git clone <repo url>
To switch to the commit:
$ git checkout <commit id>
git reset command while in detached
HEAD mode, as shown below.
$ git reset --hard
This approach is ideal if you want to play with the code without changing branch contents.
git switch - command to exit detached
In conclusion, some Git servers, like Bitbucket, allows us to fetch from a reachable commit. You will need Git version 2.5+ to take advantage of the
uploadpack.allowReachableSHA1InWant feature, which will allow you to run the
git fetch origin <commit-of-interest> command.
We have covered other alternatives that will arrive at the same destination.