<p><code>HEAD</code> is a special reference in Git that always points to "the thing" you have currently checked out. This "thing" can either be a local branch (<code>HEAD</code> is in attached mode) or a specific commit (<code>HEAD</code> is in detached mode).</p>

<p>In attached mode <code>HEAD</code> is tied to the branch it references. The current commit is determined by the position of this branch. If you move your <code>HEAD</code> around (e.g. by <a href="https://git-scm.com/docs/git-commit" rel="nofollow noreferrer">committing</a> or <a href="https://git-scm.com/docs/git-reset" rel="nofollow noreferrer">resetting</a>), in reality <code>HEAD</code> is unmodified and still just pointing to the branch and the branch is moved instead.</p>

<p>If you switch to detached mode (e.g. by <code>git checkout --detach</code> or checking out a specific commit, tag or remote branch), <code>HEAD</code> is pointing directly to the commit hash. You can continue to use Git as you did before but no branch is modified when you move your <code>HEAD</code> around. This time <code>HEAD</code> is modified directly on each such operation.</p>

<p>So to answer your specific question "<em>what are the things we can do</em>": In detached mode we can do anything we could do in attached mode (commit, merge, reset, push…) but without the need to first create a branch. We need to be careful though: When checking out another commit, nothing references the original commit anymore and it is lost*.</p>

<p>The more difficult question to answer is "<em>what is the use of it</em>". As <a href="https://devops.stackexchange.com/a/2284/2855">rasebo already stated in his answer</a>, often to quickly check out an older commit to see how the project looked like at this time. But there are more use cases. Detached mode is used during an <a href="https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History#_changing_multiple" rel="nofollow noreferrer">interactive rebase</a>, for example. Also I use it often in my personal workflow to quickly create throwaway snapshots during prototyping or experiments. I could also use temporary branches for this, but I guess it comes down to personal taste and habits.</p>

<p>To get a better feeling for this whole topic it can help to play around and observe how <code>.git/HEAD</code> and the branches in <code>.git/refs/heads/</code> are modified after executing different Git commands.</p>

<hr>

<p>* Well, not really lost, just inaccessible. It could be restored e.g. by looking up the hash in the <a href="https://git-scm.com/docs/git-reflog" rel="nofollow noreferrer">reflog</a> and then checking it out. It is only irreversibly deleted after a (manual or automatic) <a href="https://git-scm.com/docs/git-gc" rel="nofollow noreferrer">garbage collection</a> 90 days (default) in the future.</p>

CC BY-SA 4.0
2019-10-15T08:16:30.620Z
This should be the correct answer! Personally, I use temporary branch but it is good to know I can also use detached mode
Qiulang 邱朗
CC BY-SA 4.0
2018-11-11T14:05:30.857Z
2018-11-11T14:05:30.857Z
siegi
<p>The &quot;normal&quot; way of working with GIT is by checking out branches. HEAD is a &quot;pointer&quot; to the latest commit in that branch, and GIT will allow you to commit new changes after that pointer.</p>
<p>However, as you figured out, it is possible to check out a specific commit hash, if you want to. In this situation, if you make changes and try to commit them, you will be unable to do so, since you are not at HEAD, and GIT will not allow you to commit your changes.</p>
<p>Most of the time, detached HEAD (checking out an older commit) is used if you want to test how your project ran at a certain point in time.</p>
<p>If you need to make changes to the code or need to build it using Jenkins MultiBranch Pipelines, for example, it is also possible to create a new branch from that specific commit by running:</p>
<p><code>git branch my_new_fancy_branch 7y8u33ii</code></p>
<p>or</p>
<p><code>git checkout -b my_new_fancy_branch 7y8u33ii</code></p>
<p>... where <code>7y8u33ii</code> is the hash you provided in the question. For this new branch, <code>7y8u33ii</code> will be HEAD, so you will be able to commit new changes. If you need to get your code into the branch from which you created this new branch from, you will need to do a merge, but this is another discussion altogether.</p>

CC BY-SA 4.0
2018-11-11T14:07:49.153Z
The statement "_… you will be unable to do so, since you are not at HEAD, and GIT will not allow you to commit your changes_" is not correct. You can do anything you want during detached mode. See [my answer](https://devops.stackexchange.com/a/5410/2855) for details.
siegi
CC BY-SA 4.0
2017-10-10T12:18:15.883Z
2020-10-09T14:47:40.970Z
2020-10-09T14:47:40.970Z
rasebo
<p>When I checkout a specific commit hash, it goes to detached mode where HEAD POINTER can't point to a recent commit.</p>

<p>What are the things we can do and what is the use of going detached head state in Git?</p>

<git>
What is the exact use of DETACHED HEAD state in Git?
Vinodh Machi
# What is the exact use of DETACHED HEAD state in Git?

(Asked by: username_0 on 2017-10-10)

When I checkout a specific commit hash, it goes to detached mode where HEAD POINTER can't point to a recent commit.

What are the things we can do and what is the use of going detached head state in Git?

**Tags:** git


---

## 2 answers

### Answer by username_3 on 2017-10-10. Score: 8 | Accepted answer

The "normal" way of working with GIT is by checking out branches. HEAD is a "pointer" to the latest commit in that branch, and GIT will allow you to commit new changes after that pointer.

However, as you figured out, it is possible to check out a specific commit hash, if you want to. In this situation, if you make changes and try to commit them, you will be unable to do so, since you are not at HEAD, and GIT will not allow you to commit your changes.

Most of the time, detached HEAD (checking out an older commit) is used if you want to test how your project ran at a certain point in time.

If you need to make changes to the code or need to build it using Jenkins MultiBranch Pipelines, for example, it is also possible to create a new branch from that specific commit by running:

`git branch my_new_fancy_branch 7y8u33ii`

or

`git checkout -b my_new_fancy_branch 7y8u33ii`

... where `7y8u33ii` is the hash you provided in the question. For this new branch, `7y8u33ii` will be HEAD, so you will be able to commit new changes. If you need to get your code into the branch from which you created this new branch from, you will need to do a merge, but this is another discussion altogether.

> **Comments:**
> **username_1**: The statement "_… you will be unable to do so, since you are not at HEAD, and GIT will not allow you to commit your changes_" is not correct. You can do anything you want during detached mode. See [my answer](https://devops.stackexchange.com/a/5410/2855) for details.  

---

### Answer by username_1 on 2018-11-11. Score: 4

`HEAD` is a special reference in Git that always points to "the thing" you have currently checked out. This "thing" can either be a local branch (`HEAD` is in attached mode) or a specific commit (`HEAD` is in detached mode).

In attached mode `HEAD` is tied to the branch it references. The current commit is determined by the position of this branch. If you move your `HEAD` around (e.g. by [committing](https://git-scm.com/docs/git-commit) or [resetting](https://git-scm.com/docs/git-reset)), in reality `HEAD` is unmodified and still just pointing to the branch and the branch is moved instead.

If you switch to detached mode (e.g. by `git checkout --detach` or checking out a specific commit, tag or remote branch), `HEAD` is pointing directly to the commit hash. You can continue to use Git as you did before but no branch is modified when you move your `HEAD` around. This time `HEAD` is modified directly on each such operation.

So to answer your specific question "*what are the things we can do*": In detached mode we can do anything we could do in attached mode (commit, merge, reset, push…) but without the need to first create a branch. We need to be careful though: When checking out another commit, nothing references the original commit anymore and it is lost\*.

The more difficult question to answer is "*what is the use of it*". As [username_3 already stated in his answer](https://devops.stackexchange.com/a/2284/2855), often to quickly check out an older commit to see how the project looked like at this time. But there are more use cases. Detached mode is used during an [interactive rebase](https://git-scm.com/book/en/v2/Git-Tools-Rewriting-History#_changing_multiple), for example. Also I use it often in my personal workflow to quickly create throwaway snapshots during prototyping or experiments. I could also use temporary branches for this, but I guess it comes down to personal taste and habits.

To get a better feeling for this whole topic it can help to play around and observe how `.git/HEAD` and the branches in `.git/refs/heads/` are modified after executing different Git commands.

---

\* Well, not really lost, just inaccessible. It could be restored e.g. by looking up the hash in the [reflog](https://git-scm.com/docs/git-reflog) and then checking it out. It is only irreversibly deleted after a (manual or automatic) [garbage collection](https://git-scm.com/docs/git-gc) 90 days (default) in the future.

> **Comments:**
> **username_2**: This should be the correct answer! Personally, I use temporary branch but it is good to know I can also use detached mode  

---

