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

Re: About detached heads

From
APAdam Piatyszek <ediap@users.sourceforge.net>
Date
Mar 14, 2008, 12:28 UTC
Message-ID
<47DA6F89.3080609@users.sourceforge.net>
In-Reply-To
<m3lk4ly3vy.fsf@localhost.localdomain>
* Jakub Narebski [14 III 2008 11:52]:
 > Lets check what git does in each of scenarios. Let's assume that
 > current branch is named 'master'.
 >
 > At beginning we have:
 >
 >    1---2---3---4---5    <--- master <--- HEAD
 >
 > HEAD contents is "ref: refs/heads/master"
 >
 > 1. Now, "git checkout 3...", which is equivalent to "git checkout 3",
 > detaches HEAD because commit '3' is not a head (is not a branch), so
 > we have:
 >
 >    1---2---3---4---5    <--- master
 >            ^
 >             \
 >              \-------------- HEAD
 >
 > HEAD contents is "<sha1 of 3>"
 >
 >
 > 2. If we did "git reset --hard 3" we would rewind the history,
 > resulting in the following situation:
 >
 >    1---2---3           <--- master <--- HEAD
 >             \
 >              \-4---5   <... master@{1}, ORIG_HEAD, HEAD@{1}
 >
 > and now commits 4 and 5 are referenced only by reflogs, and by the
 > (temporary) "last position of HEAD" reference named ORIG_HEAD.
 >
 >
 > 3. Now, if you have published 1..5 history you would not want
 > (usually) to rewind published branch. If you do the following:
 >
 >   $ git revert --no-commit 5
 >   $ git revert 4
 >
 > you would get the following:
 >
 >    1---2---3---4---5---(5^-1 4^-1 => 3)  <--- master <--- HEAD
 >
 > git-revert applies reversal of changes in given commit, in the
 > "patch -R" ("patch --reverse") sense. Using '--no-commit' option
 > allows to squash reverting two commits into one commit. The ordering
 > of reverting ensures that there are no merge conflicts.
 >
 >
 > 4. Or you can just put the _contents_ of revision 3 into your working
 > tree, either using plumbing command git-read-tree, or by checking out
 > or resetting to top tree: "git checkout 3^{tree}", or
 > "git checkout 3 -- .", or equivalent git-reset invocation.
 >
 > This way you would get exactly
 >
 >    1---2---3---4---5---3   <--- master <--- HEAD
 >
 > but the relation of 5---3 parentage is unclear: you would have to
 > explain it in the commit mesage.

I suggest one should add the above nice explanation to FAQ or some wiki material.

BR, /Adam

-- 
.:.  Adam Piatyszek (ediap)  .:.....................................:.
.:.  ediap@users.sourceforge.net  .:................................:.
Previous: Sergei OrganovNext: Chris Shoemaker
Message 9 of 22 in “About detached heads”
  1. Geoff RussellMar 14, 2008
  2. Jonathan del StrotherMar 14, 2008
  3. David KågedalMar 14, 2008
  4. Wincent ColaiutaMar 14, 2008
  5. Matthieu MoyMar 14, 2008
  6. Wincent ColaiutaMar 14, 2008
  7. Jakub NarebskiMar 14, 2008
  8. Sergei OrganovMar 14, 2008
  9. Adam PiatyszekMar 14, 2008
  10. Chris ShoemakerMar 14, 2008
  11. Rafael Garcia-SuarezMar 14, 2008
  12. Nicolas PitreMar 14, 2008
  13. Jakub NarebskiMar 14, 2008
  14. Linus TorvaldsMar 14, 2008
  15. Björn SteinbrinkMar 14, 2008
  16. Linus TorvaldsMar 14, 2008
  17. Jakub NarebskiMar 14, 2008
  18. SeanMar 14, 2008
  19. Geoff RussellMar 14, 2008
  20. Jakub NarebskiMar 15, 2008
  21. Nicolas PitreMar 15, 2008
  22. Geoff RussellMar 15, 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.