git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: user manual question

From
Björn Steinbrink <b.steinbrink@gmx.de>
Date
Dec 29, 2008, 17:18 UTC
Message-ID
<20081229171825.GB6200@atjola.homenet>
In-Reply-To
<gjauel$94s$4@ger.gmane.org>
On 2008.12.29 16:38:30 -0000, Zorba wrote:
Show 7 quoted lines
> so a detached HEAD is just a HEAD that is not sitting on a tip ?
> 
> i.e. if I do $ git reset --hard HEAD^
> 
> ...pointing HEAD to the previous committ
> 
> this is a detached HEAD
No. At least that won't turn an "attached" HEAD into a detached HEAD.

Attached HEAD: HEAD is a symbolic ref to another ref. Detached HEAD: HEAD is a non-symbolic ref to a commit.

So you can have: HEAD -> refs/heads/master -> commitA --> attached HEAD

Or: HEAD -> commitA --> detached HEAD

Note how the commit object is the same in both cases.

What you "git reset [--hard] HEAD^" does, depends on the kind of HEAD you have.

"git reset HEAD^" will (conceptually, implementation might differ):
Resolve HEAD^ to find a commit.
and

Resolve HEAD until it finds a non-symbolic ref, and update that ref to reference the commit it found.

Looking at the cases from above:

Attached case: HEAD -> refs/heads/master -> commitA

Resolve HEAD^: HEAD^ ==> refs/heads/master^ ==> commitA^ ==> commitA_parent

Find non-symbolic ref: HEAD (symbolic) ==> refs/heads/master (non-symbolic)

So refs/heads/master gets updated to reference commitA_parent.

Now the detached case: HEAD -> commitA

Resolve HEAD^: HEAD^ ==> commitA^ ==> commitA_parent

Find non-symbolic ref: HEAD (non-symbolic)

So HEAD gets updated to reference commitA_parent.

In other words, HEAD is what tells git what you have checked out. And that's either a branch head (attached HEAD, symbolic ref) or a commit (detached HEAD, non-symbolic ref).

In the symbolic ref case, operations like "commit" or "reset" work on the branch you have checked out (found through the symbolic ref), in the non-symbolic ref case (detached HEAD), those operations work on HEAD itself.

Björn
Previous: ZorbaNext: Markus Heidelberg
Message 9 of 12 in “user manual question”
  1. ZorbaDec 28, 2008
  2. Sitaram ChamartyDec 28, 2008
  3. Dilip MDec 28, 2008
  4. ZorbaDec 29, 2008
  5. ZorbaDec 28, 2008
  6. Sitaram ChamartyDec 29, 2008
  7. Dilip MDec 29, 2008
  8. ZorbaDec 29, 2008
  9. Björn SteinbrinkDec 29, 2008
  10. Markus HeidelbergDec 29, 2008
  11. Sitaram ChamartyDec 29, 2008
  12. Daniel BarkalowDec 29, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.