{"id":3852,"date":"2020-07-08T14:51:10","date_gmt":"2020-07-08T14:51:10","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=3852"},"modified":"2020-07-08T14:51:10","modified_gmt":"2020-07-08T14:51:10","slug":"fixing-git-detached-head","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/fixing-git-detached-head\/","title":{"rendered":"Fixing Git Detached Head"},"content":{"rendered":"<p>Any checkout that is not the name of one of your branches will get you a detached HEAD. <\/p>\n<p>The above scenario could be reproduced as below<\/p>\n<ol>\n<li>lists the remote branches\n<pre>\r\ngit branch -r\r\n    origin\/Feature\/f1234\r\n    origin\/master\r\n<\/pre>\n<\/li>\n<li>I want to checkout one locally, so I cut paste:\n<pre>\r\ngit checkout origin\/Feature\/f1234\r\n<\/pre>\n<\/li>\n<li>Detached HEAD state\n<pre>\r\nYou are in 'detached HEAD' state. [...])\r\n<\/pre>\n<\/li>\n<\/ol>\n<p><strong>Solution #1:<\/strong><br \/>\nDo not include origin\/ at the front of my branch spec when checking it out:<\/p>\n<pre>\r\ngit checkout Feature\/f1234\r\n<\/pre>\n<p><strong>Solution #2:<\/strong><br \/>\nAdd -b parameter which creates a local branch from the remote<\/p>\n<pre>\r\ngit checkout -b origin\/Feature\/f1234\r\ngit checkout -b Feature\/f1234  #it will fall back to origin automatically\r\n<\/pre>\n<p>But, you have started making changes in the detached branch and you encounter that later. In such case commits will work but changes<br \/>\nwon&#8217;t be reflected in branch in remote since it is detached. To address such an issue<\/p>\n<ol>\n<li>commit changes in detached local branch<\/li>\n<li>create a new branch<\/li>\n<li>checkout the branch you are already working so the connection with remote would be again established <\/li>\n<li>Now the new branch created in local contains your changes and checkout will fetch the original remote branch without changes <\/li>\n<li>Do git merge between two and push the code to remote now<\/li>\n<\/ol>\n<pre>\r\ngit commit -m \".....\"\r\ngit branch my-temporary-work\r\ngit checkout master\r\ngit merge my-temporary-work\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Any checkout that is not the name of one of your branches will get you a detached HEAD. The above scenario could be reproduced as below lists the remote branches git branch -r origin\/Feature\/f1234 origin\/master I want to checkout one locally, so I cut paste: git checkout origin\/Feature\/f1234 Detached HEAD state You are in &#8216;detached&hellip; <a href=\"https:\/\/codethataint.com\/blog\/fixing-git-detached-head\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[163],"tags":[],"class_list":["post-3852","post","type-post","status-publish","format-standard","hentry","category-git"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=3852"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3852\/revisions"}],"predecessor-version":[{"id":3853,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/3852\/revisions\/3853"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=3852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=3852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=3852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}