62

If I do git checkout HEAD^, I get this:

$ git checkout HEAD^
Note: checking out 'HEAD^'.

You 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.

If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b new_branch_name

HEAD is now at...
$

Veteran git users are probably very familiar with this. But if I do git checkout HEAD, nothing happens:

$ git checkout HEAD
$

I'd like to create the "detached HEAD" state for the commit at the head of my current branch. How do I do that?

Russell Silva
  • 2,772
  • 3
  • 26
  • 36
  • @Mat: pretty much what the explanatory message says. I want to make some experimental commits or rebasing or amending, but I don't need to retain my changes. So I'm trying to create a throwaway copy of a branch. – Russell Silva Nov 09 '12 at 23:22
  • 2
    Why don't you just create a branch, and delete it when you're done? Same effect. – Mat Nov 10 '12 at 05:33
  • Since git1.7.5, `git checkout --detach` should work as well. See [my answer below](http://stackoverflow.com/a/19495843/6309). – VonC Oct 21 '13 at 13:27
  • 3
    It's useful when you want to delete the branch you're currently on. – Stefan Dragnev Feb 03 '15 at 15:06
  • 6
    "How to intentionally detach a head" would have been a formidable title… – o0'. Feb 23 '15 at 16:42
  • Detaching is one way to ensure a fetch works, to avoid the dreaded: `fatal: Refusing to fetch into current branch refs/heads/master of non-bare repository` – qneill Jun 30 '15 at 16:56
  • 2
    Very useful for merging changes from one machine to another, when they're on remote tracking branches but only one machine can see the other (eg laptop/server). Run `git checkout --detach` on server, then `git push server` on laptop, then `git checkout master` on server to see changes made on laptop. – Jeff Taylor Sep 20 '15 at 21:31
  • I was in the wrong branch, edited files and couldn't switch to the right branch because of conflicts. I successfully used `git checkout --detach` to create a dangling commit without polluting my `master` branch. Once in the feature branch I `cherry-pick`ed the commit by revision number. – Daniel Böhmer Jan 02 '17 at 13:46
  • Useful when you have multiple working trees and want both of them to be on the same branch, which is not possible. You can however enter detached head state in one of the working tree, which means you will be able to checkout the branch you want in the other work tree. – user14967413 Nov 02 '22 at 13:54

2 Answers2

55

Since git 1.7.5 (April 2011), you can use the git checkout --detach command.
(Since Git 2.23 (Q3 2019), you would use git switch --detach)

See commit 326696

checkout: introduce --detach synonym for "git checkout foo^{commit}"

For example, one might use this when making a temporary merge to test that two topics work well together.


Commit 8ced1aa (git 1.7.11.3, July 2012) disallows --detach on unborn branch, so this won't fail on a null HEAD:

git checkout --orphan foo
git checkout --detach
git symbolic-ref HEAD

Only the upcoming git 1.8.4.2 or 1.8.5 (Q4 2013) clarifies the syntax. See commit 26776c9:

Separate this case into two syntactical forms, mimicking the way how the DESCRIPTION section shows this usage.
Also update the text that explains the syntax to name the commit to detach HEAD at to clarify.

'git checkout' [--detach] <commit>::

Prepare to work on top of <commit>, by detaching HEAD at it (see "DETACHED HEAD" section), and updating the index and the tree will be the state recorded in the commit plus the local modifications.

  1. When the <commit> argument is a branch name, the --detach option can be used to detach HEAD at the tip of the branch (git checkout <branch> would check out that branch without detaching HEAD).

  2. Omitting <branch> detaches HEAD at the tip of the current branch.

That last point is precisely what you want to do for your current branch:

git checkout --detach
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
11

This command creates a detached head state from any given branch name (in this case, master):

git checkout master^0

Checking out commit hashes also automatically creates a detached head state, no need for ^0:

git checkout 823112f444cb4aa70032feea6e8e5eb79d0e1ed0

And of course the shorter hashes as well:

git checkout 823112f
pkamb
  • 33,281
  • 23
  • 160
  • 191
platforms
  • 2,666
  • 1
  • 18
  • 23
  • Not sure you even need the `^0` bit when you're checking out by SHA, but I haven't tested that... – twalberg Nov 08 '12 at 16:28
  • 1
    It's still useful to know that it works with commit hashes and tag names. That makes it easy to use in git-related scripts without worrying about whether you're dealing with a tag name, a commit hash or a branch name. – Wildcard Jan 22 '16 at 00:39