JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM > Git >

Clone a Git repository with a specific revision

Author:JIYIK Last Updated:2025/03/31 Views:

This article discussed various methods that we can use to clone a Git repository with a specific revision or changeset. This comes in handy when you have a repository with large files and you only need a specific version of the code.

Instead of cloning the entire repository, you can get a specific changeset, which helps reduce cloning time, especially when you are working with large files.


Clone a Git repository with a specific revision

There are several ways we can use to clone a repository with a specific changeset. Let's start with a recent development in Git that allows us to fetch the commit of interest using the git fetch command.

Prior to Git version 2.5.0, git fetch origin <commit id>this did not work. With the release of version 2.5.0, Git comes with the option to configure uploadpack.allowReachableSHA1InWant to true on the server side .

Once set to true, we can fetch any reachable commit. This feature is off by default and its use is discouraged for security and performance reasons.

Some Git servers like Bitbucket usually have this feature turned on by default. Follow the steps below to fetch a small subset of a large Git repository on a specific changeset.

We initialize a Git repository in the directory we wish to clone into.

$ git init

Then we will add our remote to the local repository.

$ git remote add origin <url://to/source/repository

We can now fetch the commit we want as follows.

$ git fetch origin <sha1-of-commit-of-interest>

Complete the process by resetting the branch to the commit of interest.

$ git reset --hard FETCH_HEAD

请注意, this will fetch the full commit history, including the specified commit. You can limit it with the '--depth=...' or '--shallow-since=...' flags.

This method is not secure and may not work on all Git servers. Is there an alternative to this method that reaches the same destination?

The method we are going to discuss is perfect for small repositories. It will still work for large repositories, but it will require some patience on your part.

Assuming you already know which commit you want to clone from, start by cloning the entire repository.

First add the remote to your local repository and proceed with cloning as shown below.

$ git clone <repo url>

Once you are done, use the git reset command to move HEAD to the commit of interest as shown below.

$ git reset --hard <commit id>

If you want to go back 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 commit:

$ git checkout <commit id>

Run the command in detached HEAD mode git resetas shown below.

$ git reset --hard

This approach is ideal if you want to work with the code without changing the contents of your branch.

Run git switchthe - command to exit detached HEAD mode.

In summary, some Git servers, like Bitbucket, allow us to fetch from reachable commits. You need Git version 2.5+ to take advantage of the uploadpack.allowReachableSHA1InWant feature, which will allow you to run git fetch origin <commit-of-interest>the command.

We have covered other alternatives that will reach the same destination.

Previous:Squash commits pushed in Git

Next: None

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Squash commits pushed in Git

Publish Date:2025/03/31 Views:86 Category:Git

This article outlines the process of squashing commits that we have pushed to a remote repository. We squash the commits into one to reduce clutter in the repository. To squash the commits, we run git rebase in interactive mode . Squash com

Git squash all commits

Publish Date:2025/03/31 Views:65 Category:Git

In every developer’s life, the word squash is often used while working with the Git distributed control system . This feature in Git is a handy option that developers often use to achieve a neat workflow in their development team. In this

Close the Git commit editor on Windows

Publish Date:2025/03/31 Views:62 Category:Git

In this article, we will discuss how to exit the Git commit editor. This can be a little tricky, especially if you are new to Git bash . Let's see how to exit the editor on Windows. Close the Git commit editor on Windows We will look at a t

Understand the Git commit sign-off function

Publish Date:2025/03/31 Views:160 Category:Git

In this article, we will explore the Git commit sign-off feature. This feature allows us to add a line containing our full name and email address to our commits. Signature confirming ownership and permission to submit submission. Understand

Add the file to the last commit in Git

Publish Date:2025/03/31 Views:176 Category:Git

This article outlines the process of adding a file to the last commit in Git. This comes in handy when you forgot to include a file in the last commit and don't want to create a new file. Let’s get straight to the point. Add the file to t

Git search commit messages using the command line

Publish Date:2025/03/31 Views:157 Category:Git

We can format git log the command to show commits with commit messages that match a specified pattern. This makes it easier when you want to find a commit but your repository has hundreds of commits. This article will discuss the process of

Modifying a specific commit in Git

Publish Date:2025/03/31 Views:100 Category:Git

This article explains how we can modify a specific commit in Git. We may need to rename, compress, edit, or add files to a commit. The best way is to use the git rebase command in interactive mode . What do you think of this? Modifying a sp

Git overwrites Master with branch

Publish Date:2025/03/31 Views:79 Category:Git

Git is used to keep track of the source code we are working with; it also facilitates collaboration and helps us keep our projects in their current state. As we develop new features, their history should be at our fingertips as it is very h

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial