Git Tools - How Do I Git Clone a Specific Commit Using Git?
Do you find yourself in a situation where you need to clone a GitHub repository but only want to retrieve a specific commit from the repository on the master branch? If yes, you can use a simple yet effective trick to get that commit. You can use git clone along with git reset to clone that particular commit and not the latest one. This trick is especially useful when you want to access a specific code version. You can access the exact code version you need by cloning a particular commit. So, if you want to clone a GitHub repository and retrieve a specific commit, this trick is for you!
git clone <REPO>
In order to get the commit we want, we first need to clone the git repository using HTTP or SSH using the git clone command. Cloning a repository creates a local copy of the remote repository, allowing you to work with the code and files locally on your machine. This is important because local copy is essential when collaborating with others on a project or contributing to an open-source project. You will need to have the repository's URL to clone a repository. Once you have cloned the repository, you can begin making changes to the code and files and then push those changes back to the remote repository for others to see and work with.
In this example, I will clone the Google Magika repository from GitHub. For those who are curious, Google Magika is an AI-powered tool from Google that detects file types using deep learning techniques to provide accurate file type detection.
cd <REPO>
After cloning the directory, we need to change our current directory to the newly cloned directory using the change directory command (cd). By doing so, we'll be able to access and modify its contents directly on our local system. This step is essential to ensure that any changes we make to the directory are applied to the local copy.
git reset --hard <Commit SHA1>
In order to effectively clone a specific git commit, we can use the git reset inside the local git repo. In order to do so, we need to pass the --hard flag along with the SHA1 hash of the commit we want to clone. To go back to a previous commit, we can use the following as an example.
In order to go back to the most recent commit, we can use the git pull command.
Conclusion
In this tutorial, we explored a handy trick for cloning a GitHub repository while specifically targeting a particular commit on the master branch. By combining the git clone command with git reset, we can efficiently retrieve the exact code version we need, avoiding the retrieval of the latest commit. This technique is invaluable if we need to access a specific code snapshot.
We demonstrated cloning the repository using its URL, navigating to the cloned directory and then employing the git reset --a hard command with the commit's SHA1 hash. This approach grants us the flexibility to work with precise code versions, which is essential for collaborating on projects or contributing to open-source initiatives.
In summary, the ability to clone a particular commit allows developers to work on a specific code snapshot, ensuring a seamless and controlled coding experience. So, the next time you find yourself needing a specific code version from a GitHub repository, remember this simple yet effective trick to streamline your workflow.