How to Clone a Specific Commit in Git? (Tutorial Example)

How to Clone a Specific Commit in Git? (Tutorial Example). In this post, we introduce Git, its working principle, advantages then show you how to clone a specific Git commit with examples.

Let’s start this article blog about how to Clone a Specific Commit in Git?

What is Git?

Image Source: Frankgwarman

From web developers to application developers, Git is helpful for anyone writing code or tracking changes in files. Git is a free, open source version control system that helps track changes in files. The design was launched by Linus Torvalds in 2005 for Linux kernel development.

All in all, Git now helps maintain a history of changes to programming code, web pages, and other types of content. At present, Junio Hamano manages and maintains this system.

Basically, the open source system also supports the data integrity feature, making it one of the popular systems. With Git, you can record all your repositories and track racking abilities. The tool has two data structures and is highly compatible with distributed workflow environment. As a result, with Git, you require no additional software. Simply, a complete solution via which you revert your files to the previous version and restore lost files.

In simple terms, Git is a file change tracker system that helps monitor every coded line. Besides,  it has the ability to manage files of any size without running up against repacking issues.

In comparison to other software or apps, Git is fast and flexible. Learn more about its benefits, features, and how exactly Git works.

How Does Git Works?

Image Source: Phoenixnap

Certainly, the open source software helps track changes in files and coordinate work on those files among multiple people. In turn, these are vast and suitable for both small and large projects.

In order to work with Git, you first need to install it on your device. The next step is to create a new repository by adding a command in the directory. Once you create the repository, check out its storage.

Create a local repository or a remote server using a specific command. Now, in the local repository, you get three options:

1. Working directory (stores original files).

2. Index (actions take place here).

3. Head (points towards the commit).

Features of Git

  • Free, scalable, and open source: Git is a free, distributed, open source, version control system (VCS) that allows storing code and tracking revision history. Also merge changes to the code and roll back to earlier versions of the code as per the need. Equally, it is free and allows multiple people to work on the same project simultaneously.
  • Easy Branching Capabilities: Branches help developers to test and run changes in the project without creating any impact on the original version. In addition, users easily test new features in the branches and later merge them with the master branch.
  • Lightweight:  There is a high chance that several users are working on the same project at the same time. Evidently, it increases the data load in the central repository. Hence, Git stores and compresses all information on the local repository during the cloning process. Thus, making it a lightweight tool that occupies minimal space.
  • Supports non linear development: With Git, users remotely perform operations on a project. Further, Git supports non linear development behavior that allows users to pick any section of the project and do the necessary operations. Once complete, they update the project. By the same token It also supports various tools for visualizing and navigating the development history.
  • Distributed System: It has a distributed System that helps developers remotely access the repository from all over the world. Using this tool, developers work simultaneously on the same project without affecting the original version.

Advantages of Git

Furthermore, Git is software that helps keep track of changes to any collection of files as well as manages projects with simple commands. Below we have listed a few advantages of Git:

  • Faster Processing: Allows you to run server and local operations faster than other software and apps. Perform operations at a high speed with Git as well.
  • More flexible: On comparing with other software or apps, Git offers more flexibility. Users can choose from the workflow options in the system.
  • Easy to Merge: Another benefit of the Git tool is it allows the merging of other codes in the system. Using this feature, developers easily interact and contribute to their system. Henceforth, it hardly takes any time to complete the merging process.
  • Well Structured Project Management: Using the tool, developers manage huge projects together and run operations faster. On comparing with other client server systems, Git runs faster.
  • Low system requirements: Even more, Git has very low system requirements, which means users run it on any computer with an Internet connection.
  • Reduces conflicts: The version control system ensures that code conflicts do not occur among several developers working in parallel. Instead of pushing their changes to all of those working on a project. Developers push their changes using the git merge command.

Up next is to learn how to Clone a Specific Commit in Git? (Tutorial Example).

How to Clone a Specific Commit in Git? (Tutorial Example)

Image Source: LinuxHint

Commit is a snapshot of all files available in the Git repository. Whenever, you make any changes in any of the file, Git creates a specific identifier. This identifier helps you to revert any specific changes that you have made.

In some cases, you may need to clone from a specific git commit id to troubleshooting any error. In this case, this tutorial  aids you in cloning a specific commit from your Git repository.

How to Checkout From Specific Git Commit ID

In this tutorial, we use the following Git repository to illustrate the article.

  • https://github.com/strapi/community-resources

Please follow the below steps to checkout from a specific commit id:

Step 1 – First, clone the full Git repository using the following command:

				
					git clone https://github.com/strapi/community-resources
				
			

Step 2 – Once the repository is downloaded to your local system, navigate to the downloaded repository using the following command:

				
					cd community-resources
				
			

Step 3 – Next, run the following command to view all commits in the master branch.

				
					git log
				
			

You should see all commits in the following output:

				
					commit 0abbf899f3a034abba82388bc3804ead1a9179ef (HEAD -> master, origin/master, origin/HEAD)
Merge: c09eae8 788468f
Author: Daniel Madalitso Phiri <malgamves@gmail.com>
Date:   Tue Aug 16 17:13:01 2022 +0200

    Merge pull request #23 from Eventyret/master
    
    Added Simen to Strapi Stars

commit 788468f9cd53a4d072c1a0f65bdc5555219a79cf
Author: Simen Daehlin <simen.dehlin@gmail.com>
Date:   Thu Aug 11 18:13:26 2022 +0100

    Added Simen to Strapi Stars

commit c09eae8a5727e4579b10be3f810d363823296a5e
Merge: 3c62691 17a4638
Author: Daniel Madalitso Phiri <malgamves@gmail.com>
Date:   Thu Feb 24 18:52:19 2022 +0200

    Merge pull request #21 from Leiksa/patch-1
    
    Add Diane GUILLOT information

commit 3c62691a4ea60ecb54c57e17d8fc1b60d2d48777
Merge: d71b5a7 3ff7faa
Author: Daniel Madalitso Phiri <malgamves@gmail.com>
Date:   Thu Jan 6 17:27:36 2022 +0200

    Merge pull request #22 from chyke007/event-meetup-grammar
    
    Fixes typos and grammatical errors
				
			

Step 4 – To clone or checkout from the specific commit, you will need the SHA1 identifier from the above log.

For example, if you want to checkout the commit 0abbf899f3a034abba82388bc3804ead1a9179ef, run the following command:

				
					git checkout 0abbf899f3a034abba82388bc3804ead1a9179ef
				
			

You will get the following output:

				
					Note: checking out '0abbf899f3a034abba82388bc3804ead1a9179ef'.

You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

HEAD is now at 0abbf89 Merge pull request #23 from Eventyret/master

				
			

Step 5 – If you want to make any changes from the commit then you will need to checkout commit in a new branch. You can do it with the following command:

				
					git checkout -b branch 0abbf899f3a034abba82388bc3804ead1a9179ef
				
			

Once the checkout is finish, you can make your required changes in the repository and save it.

Clone Git Repository From Specific Commit ID

Later you can not clone directly from a specific commit ID. But, if it is important for you then follow the below steps to clone from a specific commit ID.

Step 1 – First, clone the full repository using the following command:

				
					git clone https://github.com/strapi/community-resources
				
			

Step 2 – Next, run the following command to view all commits:

				
					git log
				
			

Step 3 – Next, run the following command to make your current HEAD pointing to the specific commit id SHA.

				
					git reset --hard 0abbf899f3a034abba82388bc3804ead1a9179ef
				
			

Set Git HEAD to Specific Commit ID

In some cases, you may need to revert your HEAD to a specific Git commit. In this case, you perform a hard reset with latest commit ID.

First, pull all changes to your repository using the following command:

				
					git pull
				
			

Next, perform a hard reset using the latest commit SHA with the following command:

				
					git reset --hard 0abbf899f3a034abba82388bc3804ead1a9179ef
				
			

That is it.  We should now summarize how to Clone a Specific Commit in Git? (Tutorial Example).

How to Clone a Specific Commit in Git? (Tutorial Example) Conclusion

To conclude, in the above post, you learned how to clone a specific commit in Git repository. Finally, Git is a free, open source distributed version control system that allows developers to manage anything from small to very large projects quickly and efficiently. What is more, it ensures that code conflicts do not occur among several developers working in parallel. For that reason, it is fast, lightweight, scalable, and an open source distributed version control system.

To read more about Gitlab content please click here

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

5 1 vote
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x