Git

How to Clone Git Repository With Specific Revision/Changeset?

A centralized version control system known as “GitHub” is necessary for an organization where multiple developers work on the same project. Git allows developers to clone a remote repository to a local repository for contributing to the development project. The cloning repository process makes copies of the remote directory onto the local system. Additionally, users are permitted to download the repositories with the specific or required revisions.

This article will explain the procedure of cloning the Git repository with specific revisions.

How to Clone Git Repository With Specific Revision/Changeset?

To clone a specific revision of the Git repository, try out the below-stated steps:

  • Go to the required local repository
  • Set remote URL to the local repository
  • Get the last commit hash of the remote repository
  • Fetch the last commit with its hash
  • Reset the repository to the desired commit

Step 1: Navigate to Repository
Move to the desired repository by utilizing the given command:

$ cd "C:\Git\new_repos"

Step 2: Copy Remote URL
On GitHub, choose your desired remote repository and copy its “HTTPS” URL to the clipboard:

Step 3: Add Remote URL to Local Repository
Run the “git remote add” command to connect the remote repository with the local repository for further processing:

$ git remote add origin https://github.com/laibayounas/demo.git

Step 4: Verify Added Remote Origin
Now, verify whether the remote origin has been added or not by executing the below command:

$ git remote -v

Step 5: Get Remote Repository Last Commit Hash
Utilize the “git ls-remote” command along with the remote URL to get the list of commit hashes from the remote repository:

$ git ls-remote https://github.com/laibayounas/demo.git

The below output shows the list of all remote repository commits. Now, select the required commit hash. For instance, we have selected the “dd9c22…” remote commit hash:

Step 6: Fetch Origin With Commit Hash
Then, download the content of origin by specifying the desired commit hash with the “git fetch” command:

$ git fetch origin dd9c220528105bdc1ad20e71904b21d5afa8885d

As you can see the required commit hash revision is fetched successfully:

Step 7: Reset Repository to Commit
Lastly, run the “git reset” command along with the “–hard” flag and fetched HEAD pointer to reset the local repository pointer to the particular remote commit:

$ git reset --hard FETCH_HEAD

We have compiled the method of cloning the Git repository with a specific revision on the local repository.

Conclusion

To clone the Git repository with specific revisions, navigate to the local Git repository. Then, open GitHub, move to a desired remote repository and copy its URL. After that, connect the local repository to the remote repository by setting the remote URL. Get the last commit hash from the remote repository and fetch it with the help of “$ git fetch origin <commit-hash>”. Lastly, run the “$ git reset –hard FETCH_HEAD” command to reset the repository HEAD pointer to that specific commit. This article illustrated the process of cloning the Git repository with a specific revision/changeset.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.