{"id":22835,"date":"2020-09-17T14:45:47","date_gmt":"2020-09-17T21:45:47","guid":{"rendered":"https:\/\/careerkarma.com\/blog\/?p=22835"},"modified":"2020-12-29T12:28:01","modified_gmt":"2020-12-29T20:28:01","slug":"git-detached-head","status":"publish","type":"post","link":"https:\/\/careerkarma.com\/blog\/git-detached-head\/","title":{"rendered":"Git Detached HEAD"},"content":{"rendered":"\n<p>While you might not encounter the problem of a detached HEAD, it is important to know about it so that you can avoid it. As a recommendation do not commit on a detached HEAD but if you do, we\u2019ll show you how to save your commits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>A Reminder<\/strong><\/h2>\n\n\n\n<p>In order to understand what the HEAD is, let&#8217;s do a refresher on Git&#8217;s underlying tree-like structure.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>The Working Directory: Or working tree. It refers to your local directory and <code>git status<\/code> will give you the state of your working directory.<\/li><li>HEAD: It is just your current branch last commit snapshot. If you were to switch branches with <code>git checkout<\/code> then the HEAD will change to the last commit on the branch.<\/li><li>Index: Or staging area, so when you <code>git add<\/code> files to commit it adds them to this index.<\/li><\/ol>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Understanding the Attached HEAD<\/strong><\/h2>\n\n\n\n<p>Now that we understand that HEAD is just the latest commit on the current branch you are on. By doing <code>git status<\/code> it will tell your branch e.g. <code>On branch master<\/code> and by doing <code>git log<\/code> it will tell your commit history with information such as:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>commit 38373004b8f651b58cea64cd629e1e2c18c164a0 (HEAD -&gt; master, origin\/master, origin\/HEAD)\nAuthor: Felipe &lt;email&gt;\nDate:   Wed Sep 29 22:57:59 2020 -0500<\/pre><\/div>\n\n\n\n<p>Then if we were to change our branch to let&#8217;s say development branch with <code>git checkout development<\/code> then the HEAD will move to the last commit. So in all these normal cases the head is following us as it is supposed to be attached to a last commit on the branch we are on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Detaching the HEAD<\/strong><\/h2>\n\n\n\n<p>There are a couple of ways we can detach our HEAD.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Using the <code>git checkout --detach<\/code> command.<\/li><li>Checkout to a commit hash. E.g. If we use the commit from above <code>git checkout 38373004b8f651b58cea64cd629e1e2c18c164a0<\/code><\/li><li>By adding ^0 on any given branch. E.g. <code>git checkout master^0<\/code>.<\/li><\/ul>\n\n\n\n<p>I&#8217;m not sure whether you&#8217;d like to intentionally detach HEAD. So the most common case is that by mistake you wanted to checkout to a branch and used a commit hash instead of the branch name.&nbsp;<\/p>\n\n\n\n<p>So what happens after a detached HEAD? You might get a warning similar to this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>$ git checkout 38373004b8f651b58cea64cd629e1e2c18c164a0\nNote: checking out '38373004b8f651b58cea64cd629e1e2c18c164a0'.\n\nYou 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.<\/pre><\/div>\n\n\n\n<p>Once on a detached HEAD environment, if you do <code>git status<\/code> even though you made changes, it will say something like this:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>HEAD detached at ac63806\nnothing to commit, working tree clean<\/pre><\/div>\n\n\n\n<p>So it will be like you are frozen in time and everything you do is kind of lost, or merely not going anywhere. Why? Well because the HEAD is not pointing to anything.&nbsp;<\/p>\n\n\n\n<p>Some people find this useful to explore a previous state of the repository without any worries of breaking things.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Re-Attaching the HEAD<\/strong><\/h2>\n\n\n\n<p>You must understand that any of your <strong>branches will not be affected if you ever get into a detached state<\/strong>. Now, the best way to reattach the HEAD is to create a new branch.&nbsp;<\/p>\n\n\n\n<p>We can do it as simple as <code>git checkout -b &lt;branch-name&gt;<\/code>.<\/p>\n\n\n\n<p>What if we didn&#8217;t realize we were without a HEAD and started making changes? Well, here we will need to create a temporary branch and just merge with the branch we need to commit. For example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre>git checkout -b temp-branch\ngit checkout master\ngit merge temp-branch<\/pre><\/div>\n\n\n\n<p>This will commit the changes from your temporary branch into the branch you need them. In this case master.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>Today we learned how to detach our HEAD (not literally) and what to do afterwards. Understanding what the HEAD is in the Git inner workings can help us understand special cases such as a detached HEAD.&nbsp;<\/p>\n\n\n\n<p>We also learned to not worry if we ever end up in a detached HEAD state.&nbsp; Just do have in mind that since your HEAD is loose, changes and commits will not be reflected anywhere. So make sure you reattached the HEAD accordingly.<br><\/p>\n","protected":false},"excerpt":{"rendered":"While you might not encounter the problem of a detached HEAD, it is important to know about it so that you can avoid it. As a recommendation do not commit on a detached HEAD but if you do, we\u2019ll show you how to save your commits. A Reminder In order to understand what the HEAD&hellip;","protected":false},"author":86,"featured_media":2789,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[17286],"tags":[],"class_list":{"0":"post-22835","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-git"},"acf":{"post_sub_title":"","sprint_id":"","query_class":"Git","school_sft":"","parent_sft":"","school_privacy_policy":"","has_review":null,"is_sponser_post":"","is_guest_post":""},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.4 (Yoast SEO v27.4) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Git Detached HEAD: A Step-By-Step Guide | Career Karma<\/title>\n<meta name=\"description\" content=\"Do you know how to detach the HEAD (not literally) and what it means in Git? Learn about this mysterious Git state with CareerKarma.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/careerkarma.com\/blog\/git-detached-head\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Git Detached HEAD\" \/>\n<meta property=\"og:description\" content=\"Do you know how to detach the HEAD (not literally) and what it means in Git? Learn about this mysterious Git state with CareerKarma.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/careerkarma.com\/blog\/git-detached-head\/\" \/>\n<meta property=\"og:site_name\" content=\"Career Karma\" \/>\n<meta property=\"article:publisher\" content=\"http:\/\/facebook.com\/careerkarmaapp\" \/>\n<meta property=\"article:published_time\" content=\"2020-09-17T21:45:47+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2020-12-29T20:28:01+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"800\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Felipe Boh\u00f3rquez\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@career_karma\" \/>\n<meta name=\"twitter:site\" content=\"@career_karma\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Felipe Boh\u00f3rquez\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/\"},\"author\":{\"name\":\"Felipe Boh\u00f3rquez\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e2cbf72dcbfaf9e81a8b6a38c1bd4220\"},\"headline\":\"Git Detached HEAD\",\"datePublished\":\"2020-09-17T21:45:47+00:00\",\"dateModified\":\"2020-12-29T20:28:01+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/\"},\"wordCount\":583,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/code-coding-connection-943096.jpg\",\"articleSection\":[\"Git\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/\",\"name\":\"Git Detached HEAD: A Step-By-Step Guide | Career Karma\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/code-coding-connection-943096.jpg\",\"datePublished\":\"2020-09-17T21:45:47+00:00\",\"dateModified\":\"2020-12-29T20:28:01+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e2cbf72dcbfaf9e81a8b6a38c1bd4220\"},\"description\":\"Do you know how to detach the HEAD (not literally) and what it means in Git? Learn about this mysterious Git state with CareerKarma.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/#primaryimage\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/code-coding-connection-943096.jpg\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2019\\\/06\\\/code-coding-connection-943096.jpg\",\"width\":1200,\"height\":800},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git-detached-head\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Blog\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Git\",\"item\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/git\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Git Detached HEAD\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/\",\"name\":\"Career Karma\",\"description\":\"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/#\\\/schema\\\/person\\\/e2cbf72dcbfaf9e81a8b6a38c1bd4220\",\"name\":\"Felipe Boh\u00f3rquez\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png\",\"contentUrl\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/wp-content\\\/uploads\\\/2020\\\/08\\\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png\",\"caption\":\"Felipe Boh\u00f3rquez\"},\"description\":\"Felipe Boh\u00f3rquez is a Software Engineer and technical writer at Career Karma. He covers all things frontend and backend development.\",\"url\":\"https:\\\/\\\/careerkarma.com\\\/blog\\\/author\\\/felipe-bohorquez\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Git Detached HEAD: A Step-By-Step Guide | Career Karma","description":"Do you know how to detach the HEAD (not literally) and what it means in Git? Learn about this mysterious Git state with CareerKarma.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/careerkarma.com\/blog\/git-detached-head\/","og_locale":"en_US","og_type":"article","og_title":"Git Detached HEAD","og_description":"Do you know how to detach the HEAD (not literally) and what it means in Git? Learn about this mysterious Git state with CareerKarma.","og_url":"https:\/\/careerkarma.com\/blog\/git-detached-head\/","og_site_name":"Career Karma","article_publisher":"http:\/\/facebook.com\/careerkarmaapp","article_published_time":"2020-09-17T21:45:47+00:00","article_modified_time":"2020-12-29T20:28:01+00:00","og_image":[{"width":1200,"height":800,"url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","type":"image\/jpeg"}],"author":"Felipe Boh\u00f3rquez","twitter_card":"summary_large_image","twitter_creator":"@career_karma","twitter_site":"@career_karma","twitter_misc":{"Written by":"Felipe Boh\u00f3rquez","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/#article","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/"},"author":{"name":"Felipe Boh\u00f3rquez","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e2cbf72dcbfaf9e81a8b6a38c1bd4220"},"headline":"Git Detached HEAD","datePublished":"2020-09-17T21:45:47+00:00","dateModified":"2020-12-29T20:28:01+00:00","mainEntityOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/"},"wordCount":583,"commentCount":0,"image":{"@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","articleSection":["Git"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/careerkarma.com\/blog\/git-detached-head\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/","url":"https:\/\/careerkarma.com\/blog\/git-detached-head\/","name":"Git Detached HEAD: A Step-By-Step Guide | Career Karma","isPartOf":{"@id":"https:\/\/careerkarma.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/#primaryimage"},"image":{"@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/#primaryimage"},"thumbnailUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","datePublished":"2020-09-17T21:45:47+00:00","dateModified":"2020-12-29T20:28:01+00:00","author":{"@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e2cbf72dcbfaf9e81a8b6a38c1bd4220"},"description":"Do you know how to detach the HEAD (not literally) and what it means in Git? Learn about this mysterious Git state with CareerKarma.","breadcrumb":{"@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/careerkarma.com\/blog\/git-detached-head\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/#primaryimage","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2019\/06\/code-coding-connection-943096.jpg","width":1200,"height":800},{"@type":"BreadcrumbList","@id":"https:\/\/careerkarma.com\/blog\/git-detached-head\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog","item":"https:\/\/careerkarma.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Git","item":"https:\/\/careerkarma.com\/blog\/git\/"},{"@type":"ListItem","position":3,"name":"Git Detached HEAD"}]},{"@type":"WebSite","@id":"https:\/\/careerkarma.com\/blog\/#website","url":"https:\/\/careerkarma.com\/blog\/","name":"Career Karma","description":"Latest Coding Bootcamp News &amp; Career Hacks from Industry Insiders","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/careerkarma.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/careerkarma.com\/blog\/#\/schema\/person\/e2cbf72dcbfaf9e81a8b6a38c1bd4220","name":"Felipe Boh\u00f3rquez","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png","url":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png","contentUrl":"https:\/\/careerkarma.com\/blog\/wp-content\/uploads\/2020\/08\/Screen-Shot-2020-08-17-at-11.10.42-AM-150x150.png","caption":"Felipe Boh\u00f3rquez"},"description":"Felipe Boh\u00f3rquez is a Software Engineer and technical writer at Career Karma. He covers all things frontend and backend development.","url":"https:\/\/careerkarma.com\/blog\/author\/felipe-bohorquez\/"}]}},"_links":{"self":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/22835","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/users\/86"}],"replies":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/comments?post=22835"}],"version-history":[{"count":0,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/posts\/22835\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media\/2789"}],"wp:attachment":[{"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/media?parent=22835"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/categories?post=22835"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/careerkarma.com\/blog\/wp-json\/wp\/v2\/tags?post=22835"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}