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

Re: About detached heads

From
Jakub Narebski <jnareb@gmail.com>
Date
Mar 14, 2008, 10:52 UTC
Message-ID
<m3lk4ly3vy.fsf@localhost.localdomain>
In-Reply-To
<93c3eada0803140246k53408c74m21f9dc277857202d@mail.gmail.com>
"Geoff Russell" <geoffrey.russell@gmail.com> writes:
Show 14 quoted lines
> This should be simple! I have a series of commits:
> 
>            1---2---3---4---5
> 
> I want to go back to 3 but not branch, so I want
> 
>            1---2---3---4---5---3
> 
> ?
> 
>          git checkout 3...
> 
> gets me the commit on a detached head, but I don't know how to put this back
> as the HEAD.

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.

HTH
-- 
Jakub Narebski
Poland
ShadeHawk on #git
Previous: Wincent ColaiutaNext: Sergei Organov
Message 7 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.