<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mansi Negi on Medium]]></title>
        <description><![CDATA[Stories by Mansi Negi on Medium]]></description>
        <link>https://medium.com/@mansinegi?source=rss-98a40e3eb8d------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*U36C5s5-jzGQCJeLonSM9A.png</url>
            <title>Stories by Mansi Negi on Medium</title>
            <link>https://medium.com/@mansinegi?source=rss-98a40e3eb8d------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 19:42:53 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@mansinegi/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[All about Git: Detached HEAD]]></title>
            <link>https://medium.com/@mansinegi/all-about-git-detached-head-19a7d2d32ca6?source=rss-98a40e3eb8d------2</link>
            <guid isPermaLink="false">https://medium.com/p/19a7d2d32ca6</guid>
            <category><![CDATA[git]]></category>
            <category><![CDATA[git-internals]]></category>
            <category><![CDATA[git-issue]]></category>
            <category><![CDATA[git-checkout]]></category>
            <category><![CDATA[git-head]]></category>
            <dc:creator><![CDATA[Mansi Negi]]></dc:creator>
            <pubDate>Mon, 10 Apr 2023 17:49:30 GMT</pubDate>
            <atom:updated>2023-04-10T17:49:30.060Z</atom:updated>
            <content:encoded><![CDATA[<p>If you have got “Detached HEAD” message while checking out a branch or even a commit, you would know exactly how scary it looks to a newbie. Having faced this quite a few times, I had never understood the whys and hows until recently. While studying the git internals, I got a better grasp of what detaching of HEAD in Git means and how to deal with it, which I will be sharing with you in this article.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fMJSNOItZFgvJJ-mo2JU2w.png" /><figcaption>Detached HEAD state on checkout a commit</figcaption></figure><p>We will cover the following:</p><ul><li>What is a Detached HEAD?</li><li>Does this mean something is wrong with my git repo?</li><li>How can we end up in this state?</li><li>How to get out of it safely?</li><li>Why (willingly!) work in a Detached HEAD state?</li></ul><h3>What is a Detached HEAD?</h3><p>If you are familiar with Git Internals, you might know that underneath git is just a <em>content-addressable</em> file system with a UI built on top of it. In simple terms “content-addressable” means, that git stores whatever kind of content we give to it with a unique key or id. The content can be retrieved back using the unique id later.</p><p>A Git repository is a collection of <em>objects</em> and <em>references</em>. Trees, blobs and commits are common objects that are stored in the git repo. They all have unique identifiers can be used to retrieve them.</p><p><em>Branches</em> are a type of reference. A branch stores a commit id that points to a commit object and it’s the latest commit in the branch. This means, when we make a new commit, the active branch gets updated and now starts pointing to this commit.</p><p><strong>HEAD</strong> in the git repo is a symbolic reference. In other words, it is a reference to another reference (that is, a branch). HEAD tells us which branch is currently checked out and we are making changes to. That is the branch where our new commit will go to. When we check out to a different branch, git updates HEAD to store a reference to that branch.</p><p>Suppose we have a project with some commits and two branches, Main and Develop. Currently, we are on the Main branch, and it points to the latest commit in the branch, the one with hash C3 :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Vfcbw5HCjrp7b3JE76Itmw.png" /><figcaption>Head points to branch “Main”</figcaption></figure><p>For readability, we will refer to the commits as C1,C2 and not their actual SHA hashes.</p><p>Now, let’s check out the Develop branch by git checkout Develop:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*7hjyPTJQlmH2d4_vYgN3_w.png" /><figcaption>Head points to branch “Develop”</figcaption></figure><p>Now, HEAD starts pointing to the checked out branch Develop. Since indirectly it is pointing to a commit (via the branch), does this mean that it can directly point to any commit?</p><p>Yes. HEAD is capable of holding a commit’s hash instead of a reference to a branch.<em> </em><strong><em>When this happens, </em></strong><strong><em>HEAD is said to be detached.</em></strong></p><p>Let’s try to check out the commit C1using: git checkout C1</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/996/1*-eBFxpr1e_LqBOFTEst2Dg.png" /><figcaption>Detached HEAD</figcaption></figure><blockquote>Our git repo has a detached HEAD now!</blockquote><p>So to summarize, when does a detached HEAD occur? <strong><em>When the </em></strong><strong><em>HEAD is pointing to a commit directly instead of pointing to a branch.</em></strong></p><h3>Is there something wrong with my repo?</h3><p>There’s nothing “wrong” with your git repo, you have not lost any changes and everything’s fine. The message simply tells us the state of the HEAD and the fact that if we make a commit in this state, we might lose that commit.</p><p>Now let’s say that we are feeling brave and we create new commits (C4, C5) in the sample project, while in the Detached HEAD state :</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*jYUK1hrehBlNTcFHcGYZ7Q.png" /></figure><blockquote>What happens if we checkout now? Are we going to lose all our commits?</blockquote><p>Let’s see what happens if we check out the develop branch again:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*hbMrz5as79-aUOv5XN-91Q.png" /></figure><p>It appears as if the commits are still there and on checking out another branch, we didn’t lose them.</p><p>Well, they are safe until the repository has not been pruned. They can be seen using the git reflog command. The commits don’t belong to any branch and hence, we will only be able to access them by their exact commit hash. So be careful with pruning the repo!</p><h3>How can we end up here?</h3><p>Some common ways of ending up in a Detached HEAD are:</p><ol><li><strong>Trying to checkout to a commit:</strong><br> Doing git checkout af4553 , where af4553 is a sample commit id.</li><li><strong>Trying to checkout to remote branch:</strong> <br>Let’s say origin is a remote to the git repo and we check out its test branch by this : git checkout origin/test. <br>Now, that’s a remote-tracking branch and it’s read-only. It can only be updated by git at the time of git push or fetch, so we cannot edit it directly. <br>How to fix this ? Create a local tracking branch : git checkout -b test origin/test . Now use this to make changes and push as needed.</li><li><strong>Checking out a tag:<br></strong>Using: git checkout v4.12 where v4.12 is the tag.</li><li><strong>Deal with git-submodules:<br></strong> Every time we do a git submodule update, the git sub-repository HEAD gets detached. This happens because the parent repo references the submodule using a commit id. Hence, git checks out the commit instead of the branch, and we have a detached HEAD.<br>One solution is to use the merge or rebaseoption with submodule update. <a href="https://stackoverflow.com/questions/18770545/why-is-my-git-submodule-head-detached-from-master/55570998#55570998">This</a> sums it up pretty well.</li></ol><p>There can be more such cases, but these are the ones that I could find.</p><h3>How to get out of it?</h3><p>Just attach the HEAD again! Make it point to an actual branch.</p><p><strong>Wait!</strong> Have you made any changes or commits, while in the Detached HEAD state?</p><p>Here’s how to deal with it then:</p><ol><li>You have committed nothing or don’t want to save your changes after committing: <br> Check out an existing branch : git checkout main</li><li>You committed some changes and you want to save those commits:<br>git checkout -b &quot;new-branch-name&quot;: Creates a new branch and check it out. Now, this branch has the latest commit you made while in the Detached HEAD state. <br>git branch &quot;new-branch-name”: Only creates a new branch at your latest commit and doesn’t check out it, so you will still see a detached HEAD. You need to checkout to the new branch to remove that warning.</li></ol><h4>What if you checked out another branch and need those old commits now?</h4><p>Try using git reflog to find the id of the last commit that you made while in the detached state. If the git repo was pruned, you might not be able to find them.</p><p>Say, it was C5 as in our example. Our options are:<br>1. Create a new branch using git branch &quot;new-branch-name&quot; C5<br>2. Check out C5 and use git checkout -b &quot;new-branch-name&quot; to create a new branch.<br>3. Use git cherry-pick -n C5 to pick up changes from that commit and push them to the current snapshot.</p><p>Suppose, we need our commits C4 and C5 and so we create a new branch, “Feature” at C5: git branch Feature C5</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*jFiLsEyx8K8J7nCgQoaBzA.png" /></figure><blockquote>Now we can easily checkout the Feature branch, to get back to our commits!</blockquote><h3>Why detach the HEAD willingly?</h3><p>There might be some advantages of being in a Detached HEAD state after all :</p><ul><li>If we have a branch on the remote repo, we can just check out the remote-tracking branch in our local and play around with it easily.</li><li>Let’s say we are just wandering around in the project’s history. Perhaps we saw a commit and want to start another branch at this particular snapshot. But we also want to verify if everything is good at that commit, for example: tests are not failing etc.</li><li>When we commit something in the detached state, we don’t have those changes in the project’s history, unless we want them. While we do have that commit saved for some time and can retrieve it, we don’t have to and it will get removed at the time of pruning.</li></ul><p>That was enough detaching HEAD(s) for today! Please feel free to add suggestions/improvements. Constructive criticism is always welcome :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=19a7d2d32ca6" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>