Git

How to Git clone Particular Version of Remote Repository

Sometimes Git developers want to connect the local repository with the GitHub server to push the updated content of the local machine. In such situations, first, they need to build a connection between them. After that, the cloning operation can be performed. Additionally, they are allowed to clone a particular version of the remote repository.

This post will discuss the method of cloning the desired version of the remote repository on Git.

How to Git Clone Particular Version of Remote Repository?

Implement the below-stated instructions to clone the desired version of the remote repository on Git:

  • Switch to the required repository.
  • Set the remote URL to the list.
  • Clone the GitHub server repository to the particular folder in the local repository.
  • Navigate to the cloned repository folder, check the log history, and select commit id.
  • Execute the β€œgit reset HEAD <commit-id>” command.
  • Verify the log history.

Step 1: Switch to Git Repository
At first, move to the required Git local repository with the help of the β€œcd” command:

$ cd "C:\Users\nazma\Git\perk3"

Step 2: Add Remote URL
Use the β€œ git remote add” command to add the remote URL in the list to build a connection between the local and remote server for tracking:

$ git remote add origin https://github.com/GitUser0422/demo5.git

Here, the β€œorigin” is specified as the name of the remote:

Step 3: Verify Remote List
Next, to ensure that the specified remote repository URL is added or not, run the provided command:

$ git remote -v

It can be seen that the β€œorigin” exists as the remote URL name in the list:

Step 4: Clone GitHub Repository
Then, execute the β€œgit clone” command for cloning the remote repository to the local machine:

$ git clone https://github.com/GitUser0422/demo5.git remote_repo

In the above-given command, we have specified the name of the folder at the end of the remote repository path in which we want to clone. For instance, β€œremote_repo”:

Step 5: Check Git Log History
After that, view the history of the Git log by running the β€œgit log” command along with the β€œ–oneline” option:

$ git log --oneline

From the provided log history, we have selected the highlighted commit id:

Step 6: Navigate to Cloned Remote Folder
Now, switch to the folder in which the cloned remote repository is placed by executing the β€œcd” command:

$ cd remote_repo

Step 7: Reset HEAD to Particular Commit
Finally, run the β€œgit reset” command and reset the HEAD pointer to the desired version of the remote repository:

$ git reset --hard 5093141

According to the below-given output, the HEAD pointer is moved on the specified commit id:

Step 8: Ensure Cloned Remote Repository Particular Version
Lastly, verify the particular version of the remote repository is cloned or not by checking its log history:

$ git log --oneline

It can be observed that the cloned remote repository folder contains the desired version:

That’s it! You have learned how to clone the desired version of the remote repository on Git.

Conclusion

To clone the desired version of the remote repository on Git, first, move to the required repository and add the remote URL to the list. Then, clone the GitHub repository to the specific folder in the local machine. After that, navigate to the cloned repository folder, check the log history and select commit id. Next, execute the β€œgit reset HEAD <commit-id>” command and verify it by checking the log history. This post demonstrated the process of cloning the desired version of the remote repository on Git.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.