{"id":335944,"date":"2021-09-07T14:45:47","date_gmt":"2021-09-07T11:45:47","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/how-do-you-fix-a-detached-head-in-a-git-repository\/"},"modified":"2021-09-07T14:45:47","modified_gmt":"2021-09-07T11:45:47","slug":"how-do-you-fix-a-detached-head-in-a-git-repository","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/how-do-you-fix-a-detached-head-in-a-git-repository\/","title":{"rendered":"#How Do You Fix a \u201cDetached HEAD\u201d in a Git Repository?"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2a27731a73a\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2a27731a73a\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/how-do-you-fix-a-detached-head-in-a-git-repository\/#What_Is_The_Git_HEAD\" >What Is The Git HEAD?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/how-do-you-fix-a-detached-head-in-a-git-repository\/#If_You_Got_Here_By_Accident\" >If You Got Here By Accident<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/how-do-you-fix-a-detached-head-in-a-git-repository\/#If_You_Want_To_Save_Your_Changes\" >If You Want To Save Your Changes<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/how-do-you-fix-a-detached-head-in-a-git-repository\/#Rebasing_Instead_of_Merging\" >Rebasing Instead of Merging<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#How Do You Fix a \u201cDetached HEAD\u201d in a Git Repository?&#8221;<\/strong><br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"type:primaryImage alignnone size-full wp-image-2266\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png?width=1198&amp;trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"Git logo\" width=\"700\" height=\"299\" \/><\/p>\n<p>One of the regular error messages you&#8217;re likely to encounter with Git is the warning that &#8220;<em>you are in &#8216;detached HEAD&#8217; state.<\/em>&#8221; You may have stumbled into this accidentally, but don&#8217;t worry&#8212;it&#8217;s a normal problem, and can easily be fixed once you understand what it means.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"What_Is_The_Git_HEAD\"><\/span>What Is The Git HEAD?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>&#8220;HEAD&#8221; is simply an alias for your current working commit, much like your current directory on a command line. Whatever state your Git repository is in, HEAD always points to something, and new commits will be <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">app<\/a>ended in front of the HEAD.<\/p>\n<p>Usually, HEAD doesn&#8217;t directly reference a single commit. It instead points at a branch, and indirectly references the latest commit in that branch. Whenever you &#8220;checkout&#8221; a new branch, Git switches the HEAD over to that branch. This allows you to make new commits that will be appended to the end of that branch.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14015\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/82fcdee0.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1165\" height=\"604\" \/><\/p>\n<p>However, you can also check out individual commits. What if you wanted to examine the repository at a specific point in time? That&#8217;s one of the major upsides of Git, but it presents a slight problem with how Git structures things. Since new commits won&#8217;t update a branch, any commits you make after moving the HEAD away will become detached from all the branch references, hence, &#8220;detached HEAD.&#8221;<\/p>\n<p>This can actually be fairly useful. Say you moved the HEAD back a few commits, and then made some experimental updates. You&#8217;re basically making a new branch, which could get merged back into <code>master<\/code>\u00a0later on.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14017\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/8cf6cc09.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1141\" height=\"596\" \/><\/p>\n<p>You&#8217;ll need to officially integrate it into the Git repo if you do decide to keep the changes, but as a tool for doing back-in-time modifications, having a detached HEAD isn&#8217;t as scary as it sounds.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"If_You_Got_Here_By_Accident\"><\/span>If You Got Here By Accident<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>It&#8217;s very easy to end up here on accident, and be confused about the error message Git is complaining about despite having done nothing wrong. Luckily, it&#8217;s incredibly easy to fix:<\/p>\n<p>If you just checked out a commit, and haven&#8217;t touched the repository otherwise (no new commits were made), you can simply move the HEAD back to where it belongs with <code>git checkout<\/code>:<\/p>\n<pre>git checkout master<\/pre>\n<p>This is basically like running <code>cd<\/code>\u00a0on Linux, except Git still associates cached commits and changes with the old HEAD, so you don&#8217;t want to checkout if you have commits or changes that would be left hanging.<\/p>\n<p>If you did have some changes, and want to just throw them away, you can hard reset back to <code>master<\/code>:<\/p>\n<pre>git reset --hard origin\/master\ngit clean -d --force<\/pre>\n<p>If you want to save your commits though, you&#8217;ll need to officially merge them back into Git&#8217;s timeline.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"If_You_Want_To_Save_Your_Changes\"><\/span>If You Want To Save Your Changes<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>The first thing you&#8217;ll want to do if you want to keep the changes you made while in a detached HEAD state is to make a new branch. This is because Git&#8217;s garbage collector will clean up detached commits automatically, so you don&#8217;t ever want to lose track of them.<\/p>\n<pre>git branch detached-branch<\/pre>\n<p>Then you can checkout this branch to move the HEAD so it is no longer detached, and instead pointed at this official new branch:<\/p>\n<pre>git checkout detached-branch<\/pre>\n<p>Once the changes are recorded, you have one of two options. This new branch is basically an ordinary feature branch, so you can either <code>git merge<\/code>\u00a0or <code>git rebase<\/code>.<\/p>\n<p>Merging is straightforward; checkout master, and merge the detached branch:<\/p>\n<pre>git checkout master\ngit merge detached-branch<\/pre>\n<p>This works well if you&#8217;re integrating into a branch that needs approval, as is the case with pull requests and code reviews. You may need to go through your team&#8217;s approval process rather than running these commands yourself.<\/p>\n<p>If you do have access, like if you&#8217;re merging back into a feature branch you&#8217;re working on, a better method is to cherry pick the detached commits. Cherry picking basically copies a commit, and pastes it onto another branch. It&#8217;s usually used for pulling out specific commits from a feature branch before the whole thing is ready, but in this case, cherry-picking can reconcile the detached branch without a merge.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-14018\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/b248d5c6.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"1057\" height=\"503\" \/><\/p>\n<p>In this example, the commit up above is detached. Rather than merging it, it&#8217;s cherry-picked and moved onto the\u00a0<code>feature<\/code>\u00a0branch. This moves the HEAD and feature branch to point to the latest commit without a merge. Then, the original detached commit can be discarded.<\/p>\n<p>First, you&#8217;ll need to make the detached branch, and then checkout the feature branch to move the HEAD there:<\/p>\n<pre>git branch detached-branch\ngit checkout feature<\/pre>\n<p>Then run Git log to get a list of commits:<\/p>\n<pre>git log --pretty=format:\"%h %s\" --graph<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-13234\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2021\/08\/7f8a78ad.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"883\" height=\"375\" \/><\/p>\n<p>Then you can cherry-pick a commit by its ID:<\/p>\n<pre>git cherry-pick 1da76d3<\/pre>\n<p>Then, once you&#8217;ve confirmed that the cherry-picked commits are applied properly, you can delete the detached branch, since the commits were copied and it is no longer used.<\/p>\n<pre>git branch -D detached-branch<\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Rebasing_Instead_of_Merging\"><\/span>Rebasing Instead of Merging<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>You could also do a\u00a0<em>rebase<\/em>. Rebases are different from merges in that they rewrite the branch history, lifting up the detached commits and moving them to the front of the branch.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-866\" src=\"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/06\/86db9305.png?trim=1,1&amp;bg-color=000&amp;pad=1,1\" alt=\"\" width=\"700\" height=\"200\" \/><\/p>\n<p>However, the problem with this is that you&#8217;re still left with two branches, and you will still need to merge <code>feature<\/code>\u00a0and <code>detached-branch<\/code>\u00a0together, leaving you with a merge commit either way. But, it does leave the repository with a more linear Git history, which is preferable to running <code>git merge<\/code>\u00a0if you have the authority to do so.<\/p>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/14012\/how-do-you-fix-a-detached-head-in-a-git-repository\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#How Do You Fix a \u201cDetached HEAD\u201d in a Git Repository?&#8221; One of the regular error messages you&#8217;re likely to encounter with Git is the warning that &#8220;you are in &#8216;detached HEAD&#8217; state.&#8221; You may have stumbled into this accidentally, but don&#8217;t worry&#8212;it&#8217;s a normal problem, and can easily be fixed once you understand what&#8230;<\/p>\n","protected":false},"author":1,"featured_media":335945,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2019\/10\/e713ed70-1.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-335944","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/335944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=335944"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/335944\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/335945"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=335944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=335944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=335944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}