Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I intentionally detach HEAD in git?

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?

like image 432
Russell Silva Avatar asked Nov 08 '12 16:11

Russell Silva


People also ask

How do you push your head detached?

Detached head usually means that the branch you checkout to does not has the latest commit. So, basically you need to adjust the HEAD of your current branch to the latest commit. There are usually 2 ways to do it. You can create a new branch, push your code to that branch (this will pull your detached code too).

What causes git detached head?

A detached HEAD occurs when you are viewing a single commit in the history of a Git repository. You'll see a message whenever you enter into detached HEAD state informing you that you are no longer on a branch.


1 Answers

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 
like image 72
VonC Avatar answered Oct 06 '22 09:10

VonC