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

Re: for newbs = little exercise / tutorial / warmup for windows and other non-sophisticated new Git users :-)

From
Sitaram Chamarty <sitaramc@gmail.com>
Date
Dec 30, 2008, 05:34 UTC
Message-ID
<slrngljcne.n7q.sitaramc@sitaramc.homelinux.net>
In-Reply-To
<gjc7qa$jdj$4@ger.gmane.org>
Hi Conor,
On 2008-12-30, Zorba <cr@altmore.co.uk> wrote:
> so when I reset to A, I've still got the ability to get to B or C again
if this is true...  (call this case 1)
> Now I appreciate that if I commit a new change from versionA (lets call it 
> B1), then HEAD is now at B1, and B, C etc are lost, correct ?
...then this (call this case 2) is not true.

In case 1, *how* do you get back to B and C? However you do it, the same action will get you to B and C *after* you created B1 as well.

It might help to think of these things loosely using a malloc/linked-list analogy. Imagine a singly-linked list, and a variable in your program called "head" pointing to the top element. Each element contains a back-pointer to its parent. You can start from head and walk the list backward, but you can't walk forward -- there are no forward pointers.

A "git commit" is analogous to:
    newblkptr = malloc(...);
    ... store whatever you want in the new block ...
    newblkptr.backptr = head;
    head = newblkptr;
i.e., a classic linked list add.
A "git reset --hard HEAD^" essentially pops the topmost
element off the list:
    head = head.backptr

A "git checkout -f HEAD^" is like walking a linked list backward in a loop, and stopping at some intermediate block to do something -- other the temporary loop-variable, nothing else (no global variable perhaps) points to this block, so this is like a detached head.

Of course, nothing prevents you from permanently saving this
temporary loop variable because you suddenly realise *this*
link in the chain is very important for the rest of your
program:
    some_global_var = temp_blk_ptr;
This is like "git checkout -b newbranch" does.
> Its pertinent to where I am right now, as I've goofed a commit, and want to 
> reset, and commit again but I'm worried about leaveing garbage lying around 
> (the commits for version B and C in the example above).

(1) never worry about leaving garbage around; there are many operations that do that; it's part of the design. Don't sweat it.

(2) when in doubt, try "gitk --all". That's a good GUI. I'm normally a CLI freak, but when something needs to be understood/learnt, a well-designed GUI really helps.

Run "gitk --all" on the side and hit F5 on it after each operation on the command line -- helped me when I was trying to figure out what's happening.

PS: although it doesn't mention detached heads,
http://eagain.net/articles/git-for-computer-scientists/
helped me a lot...
Previous: ZorbaNext: Zorba
Message 7 of 21 in “for newbs = little exercise / tutorial / warmup for windows and other non-sophisticated new Git users :-)”
  1. ZorbaDec 27, 2008
  2. ZorbaDec 28, 2008
  3. Jeff WhitesideDec 29, 2008
  4. ZorbaDec 30, 2008
  5. Jeff WhitesideDec 30, 2008
  6. ZorbaDec 30, 2008
  7. Sitaram ChamartyDec 30, 2008
  8. ZorbaDec 30, 2008
  9. ZorbaDec 30, 2008
  10. ZorbaDec 30, 2008
  11. Jeff WhitesideDec 30, 2008
  12. ZorbaDec 30, 2008
  13. Jeff WhitesideDec 30, 2008
  14. Daniel BarkalowDec 30, 2008
  15. ZorbaDec 31, 2008
  16. ZorbaDec 30, 2008
  17. Boyd Stephen Smith Jr.Dec 30, 2008
  18. Boyd Stephen Smith Jr.Dec 30, 2008
  19. Boyd Stephen Smith Jr.Dec 30, 2008
  20. Sitaram ChamartyDec 31, 2008
  21. Daniel BarkalowDec 30, 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.