{"id":948,"date":"2021-03-02T11:33:49","date_gmt":"2021-03-02T11:33:49","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=948"},"modified":"2021-03-02T11:33:49","modified_gmt":"2021-03-02T11:33:49","slug":"checkout-clone-specific-git-commit-id-sha","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=948","title":{"rendered":"How to Checkout\/Clone From a Specific Git Commit Id (SHA)"},"content":{"rendered":"<p>There are scenarios where you might need to checkout or clone from a specific git commit id. For example, you might want to perform a git pull on a specific commit version for troubleshooting.<\/p>\n<p>This blog explains the steps involved in checking out a specific git commit ID (SHA).<\/p>\n<div class=\"kg-card kg-callout-card kg-callout-card-grey\">\n<div class=\"kg-callout-text\"><b><strong style=\"white-space: pre-wrap;\">Important Note:<\/strong><\/b> Always keep a backup of your code with all the commit histories before performing any git activity that may change or reverse the code.<\/div>\n<\/div>\n<h2 id=\"checkout-from-specific-git-commit-id\">Checkout From Specific Git Commit ID<\/h2>\n<p>Follow the steps to checkout from a specific commit id.<\/p>\n<p><strong>Step 1:<\/strong> Clone the repository or fetch all the latest changes and commits.<\/p>\n<p><strong>Step 2: <\/strong>Get the commit ID (SHA) that you want to checkout. From your local repository, you can get the commit SHA from the log. If you are using any platforms like Github, you can get the SHA id from the GUI itself.<\/p>\n<pre><code>git log<\/code><\/pre>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/git-commit-ids-min-1.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"571\" height=\"317\"><\/figure>\n<p><strong>Step 3:<\/strong> Copy the commit (SHA) id and checkout using the following command.<\/p>\n<pre><code>git checkout 28cd74decb47d8a5294eb59e793cb24a0f242e9e<\/code><\/pre>\n<p>You can also use the short form of the SHA ID from the start, as shown below.<\/p>\n<pre><code>git checkout 28cd74de<\/code><\/pre>\n<p>You need to be <strong>very careful with this checkout<\/strong>. It will be in the <strong>detached HEAD mode<\/strong>. Meaning, you can play around with making the changes without impacting any branches. So if you need to make any actual changes to a specific commit,  use a branch checkout as explained in the next step.<\/p>\n<p><strong>Step 4:<\/strong> If you want to make changes from the commit ID checkout, you need to <strong>create a branch<\/strong>, as shown below.<\/p>\n<pre><code>git checkout -b &lt;new-branch-name&gt; &lt;commit-id-sha&gt;<\/code><\/pre>\n<p>For example,<\/p>\n<pre><code>git checkout -b test-branch 7d4c59f5<\/code><\/pre>\n<p>This will retain everything from the commit ID to the <code>test-branch<\/code>.<\/p>\n<h2 id=\"git-clone-from-specific-commit-id\">Git Clone From Specific Commit ID<\/h2>\n<p>There is no direct way to clone directly using the commit ID. But you can<a href=\"https:\/\/devopscube.com\/checkout-clone-git-tags\/\" rel=\"noreferrer noopener\"> clone from a git tag<\/a>.<\/p>\n<p>However, you can do the following workaround to perform a clone if it is really necessary.<\/p>\n<ol>\n<li>Clone the repository<\/li>\n<li>Perform a hard reset with commit SHA id<\/li>\n<\/ol>\n<p>The above steps will make your current HEAD pointing to the specific commit id SHA.<\/p>\n<p>For example,<\/p>\n<pre><code>\ngit clone &lt;repository&gt;\n \ngit reset --hard &lt;COMMIT-SHA-ID&gt;\n <\/code><\/pre>\n<h2 id=\"set-git-head-to-specific-commit-id\">Set Git HEAD to Specific Commit ID<\/h2>\n<p>If you want to revert your HEAD to a specific commit, perform a hard reset with the latest commit SHA after pulling the changes, as shown below.<\/p>\n<pre><code>git pull\ngit reset --hard 7d4c59f5<\/code><\/pre>\n<p>To revert it back, you can perform a git pull that will get all the changes from the <a href=\"https:\/\/devopscube.com\/set-git-upstream-respository-branch\/\" rel=\"noreferrer noopener\">upstream branch<\/a>.<\/p>\n<p>Also, learn how to <a href=\"https:\/\/devopscube.com\/checkout-git-pull-request\/\" rel=\"noreferrer noopener\">checkout a git pull request<\/a>.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/checkout-clone-specific-git-commit-id-sha\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Checkout\/Clone From a Specific Git Commit Id (SHA) \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/checkout-clone-specific-git-commit-id-sha\/<\/p>\n","protected":false},"author":1,"featured_media":949,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-948","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/948","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=948"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/948\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/949"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=948"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=948"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=948"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}