{"thread":{"id":"29","subject":"git for beginners","startedAt":"2005-04-14T23:19:00Z","lastAt":"2005-04-14T23:19:00Z","messageCount":1,"participants":["tony.luck@intel.com"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"160","messageId":"200504142319.j3ENJ0O05860@unix-os.sc.intel.com","threadId":"29","inReplyTo":null,"subject":"git for beginners","fromName":"","fromEmail":"tony.luck@intel.com","sentAt":"2005-04-14T23:19:00Z","receivedAt":"2005-04-14T23:19:00Z","isPatch":false,"sender":{"key":"tony.luck@intel.com","avatar":"https://avatars.githubusercontent.com/u/5446021?v=4"},"body":"If the mechanics of git still have you stumped, then here are a couple of\nexamples of using the really low level tools to do some basic operations.\nIn real life you would have higher level wrappers around these tools so\nthat you don't have to remember to update .git/HEAD with the new SHA1 after\nyou do a commit ... but knowing what the low-level tools do may help you\nunderstand what is going on.\n\n-Tony\n\n$ # How to create your own project from scratch\n$ mkdir tmp\n$ cd tmp\n$ # Create initial empty git filesystem\n$ init-db \ndefaulting to private storage area\n$ # Look around ... lots of directories, but no files yet\n$ ls -l .git\ntotal 4\ndrwxr-xr-x  258 aegl aegl 4096 Apr 14 14:54 objects\n$ ls .git/objects/\n00  09  12  1b  24  2d  36  3f  48  51  5a  63  6c  75  7e  87  90  99  a2  ab  b4  bd  c6  cf  d8  e1  ea  f3  fc\n01  0a  13  1c  25  2e  37  40  49  52  5b  64  6d  76  7f  88  91  9a  a3  ac  b5  be  c7  d0  d9  e2  eb  f4  fd\n02  0b  14  1d  26  2f  38  41  4a  53  5c  65  6e  77  80  89  92  9b  a4  ad  b6  bf  c8  d1  da  e3  ec  f5  fe\n03  0c  15  1e  27  30  39  42  4b  54  5d  66  6f  78  81  8a  93  9c  a5  ae  b7  c0  c9  d2  db  e4  ed  f6  ff\n04  0d  16  1f  28  31  3a  43  4c  55  5e  67  70  79  82  8b  94  9d  a6  af  b8  c1  ca  d3  dc  e5  ee  f7\n05  0e  17  20  29  32  3b  44  4d  56  5f  68  71  7a  83  8c  95  9e  a7  b0  b9  c2  cb  d4  dd  e6  ef  f8\n06  0f  18  21  2a  33  3c  45  4e  57  60  69  72  7b  84  8d  96  9f  a8  b1  ba  c3  cc  d5  de  e7  f0  f9\n07  10  19  22  2b  34  3d  46  4f  58  61  6a  73  7c  85  8e  97  a0  a9  b2  bb  c4  cd  d6  df  e8  f1  fa\n08  11  1a  23  2c  35  3e  47  50  59  62  6b  74  7d  86  8f  98  a1  aa  b3  bc  c5  ce  d7  e0  e9  f2  fb\n$ find . -type f\n$\n$ # create some files ready to check in ... use a subdirectory just to show\n$ # how subdirs work\n$ mkdir src\n$ cat > src/hello.c\n#include <stdio.h>\n\nmain()\n{\n\tprintf(\"Hello, world!\\n\");\n}\n$ cat > Makefile\nhello: src/hello.c\n\tcc -o hello -O src/hello.c\n$ # Now add these two files to the cache, ready for checkin (use the\n$ # \"--add\" option because these files are new)\n$ update-cache --add Makefile src/hello.c\n$ # save a tree listing these files\n$ write-tree\neab75ce51622aa312bb0b03572d43769f420c347\n$ # commit the change. We tell it the SHA1 of the tree we just made\n$ commit-tree eab75ce51622aa312bb0b03572d43769f420c347\nCommitting initial tree eab75ce51622aa312bb0b03572d43769f420c347\nFirst revision of the hello system.\n0107d57e748b2f01601adb6749a03aed7b3f5a84\n$ # Save the SHA1 for that changeset ... we need it later\n$ echo 0107d57e748b2f01601adb6749a03aed7b3f5a84 > .git/HEAD\n$ \n$ # Take a look at the changeset\n$ cat-file commit 0107d57e748b2f01601adb6749a03aed7b3f5a84\ntree eab75ce51622aa312bb0b03572d43769f420c347\nauthor Tony Luck <aegl@example.com> Thu Apr 14 14:57:27 2005\ncommitter Tony Luck <aegl@example.com> Thu Apr 14 14:57:27 2005\n\nFirst revision of the hello system.\n$ # And dig into the tree we saved\n$ ls-tree eab75ce51622aa312bb0b03572d43769f420c347\n100664\tblob\t3a7a1c51dbc62797d6c903203de44cc6a734c05c\tMakefile\n40000\ttree\tba103f91defa4b3885b826d6630a055f27800398\tsrc\n$ # see that git automatically made a tree for the \"src\" subdir, look at it\n$ ls-tree ba103f91defa4b3885b826d6630a055f27800398\n100664\tblob\t522fff361ad5c07351479ea8504b7c370d189524\thello.c\n$ # This blob is our src/hello.c file\n$ cat-file blob 522fff361ad5c07351479ea8504b7c370d189524\n#include <stdio.h>\n\nmain()\n{\n\tprintf(\"Hello, world!\\n\");\n}\n$ # Look at all the files we have now, a blob for each file, a\n$ # pair of tree objects for the directory and subdir, and a lone\n$ # changeset.\n$ find .git -type f\n.git/index\n.git/objects/01/07d57e748b2f01601adb6749a03aed7b3f5a84\n.git/objects/ba/103f91defa4b3885b826d6630a055f27800398\n.git/objects/ea/b75ce51622aa312bb0b03572d43769f420c347\n.git/objects/52/2fff361ad5c07351479ea8504b7c370d189524\n.git/objects/3a/7a1c51dbc62797d6c903203de44cc6a734c05c\n.git/HEAD\n$ # Now make a change\n$ ed src/hello.c\n59\n$i\n\treturn 0;\n.\nw\n70\nq\n$ # We need to tell .git/index which file(s) are going to be\n$ # in this changeset. Don't need \"--add\" option because we are\n$ # changing a file that already exists\n$ update-cache src/hello.c \n$ # Now we can write a new tree incorporating the change\n$ write-tree\n8f5ba0203e31204c5c052d995a5b4449226bcfb5\n$ # and finally create a changeset ... this time we tell commit that\n$ # the parent of this change is the previous change\n$ commit-tree 8f5ba0203e31204c5c052d995a5b4449226bcfb5 -p `cat .git/HEAD`\nFix hello program to return successful exit code.\n5403689e0c29607f57da8f751d4ba40637134e87\n$ # save the new changeset in .git/HEAD\n$ echo 5403689e0c29607f57da8f751d4ba40637134e87 > .git/HEAD \n$ # walk the tree from HEAD to the new version of hello.c\n$ cat-file commit 5403689e0c29607f57da8f751d4ba40637134e87\ntree 8f5ba0203e31204c5c052d995a5b4449226bcfb5\nparent 0107d57e748b2f01601adb6749a03aed7b3f5a84\nauthor Tony Luck <aegl@example.com> Thu Apr 14 15:00:34 2005\ncommitter Tony Luck <aegl@example.com> Thu Apr 14 15:00:34 2005\n\nFix hello program to return successful exit code.\n$ ls-tree 8f5ba0203e31204c5c052d995a5b4449226bcfb5\n100664\tblob\t3a7a1c51dbc62797d6c903203de44cc6a734c05c\tMakefile\n40000\ttree\t77dc2cb94930017f62b55b9706cbadda8c90f650\tsrc\n$ ls-tree 77dc2cb94930017f62b55b9706cbadda8c90f650\n100664\tblob\t8a6a2a7261742c6f69adaa8c876045e721ffff22\thello.c\n$ cat-file blob 8a6a2a7261742c6f69adaa8c876045e721ffff22\n#include <stdio.h>\n\nmain()\n{\n\tprintf(\"Hello, world!\\n\");\n\treturn 0;\n}\n$ \n\n\n$ # Now an example of getting started with a pre-existing project\n$ # download www.kernel.org/pub/linux/kernel/people/torvalds/sparse.git\n$ # then ...\n$ ls -l sparse.git\ntotal 8\n-rw-rw-r--    1 aegl aegl   41 Apr 14 14:50 HEAD\ndrwxr-xr-x  258 aegl aegl 4096 Apr 12 21:33 objects\n$ # set up environment so that git sees these objects when we are elsewhere\n$ export SHA1_FILE_DIRECTORY=`pwd`/sparse.git/objects\n$ # make a directory to work in\n$ mkdir sparse\n$ cd sparse\n$ # Still need a local .git for the cache file\n$ mkdir .git\n$ # Now look at the most recent commit, to find the topmost tree\n$ cat-file commit `cat ../sparse.git/HEAD`\ntree 67607f05a66e36b2f038c77cfb61350d2110f7e8\nparent 9c59995fef9b52386e5f7242f44720a7aca287d7\nauthor Christopher Li <sparse@chrisli.org> Sat Apr  2 09:30:09 PST 2005\ncommitter Linus Torvalds <torvalds@ppc970.osdl.org> Thu Apr  7 20:06:31 2005\n\n[PATCH] static declear\n\nThis patch add static declare to make sparse happy of checking itself.\n$ # load up that tree into our cache (.git/index)\n$ read-tree 67607f05a66e36b2f038c77cfb61350d2110f7e8\n$ # and checkout all the files\n$ checkout-cache -a\n$ # quick as a flash, our directory is full of files\n$ ls\nallocate.c       compat-linux.c    example.c     inline.c     memops.c       scope.c       symbol.h          tokenize.c\nallocate.h       compat-mingw.c    expand.c      lib.c        obfuscate.c    scope.h       target.c          validation\nbitmap.h         compat-solaris.c  expression.c  lib.h        parse.c        show-parse.c  target.h\ncgcc             compile.c         expression.h  LICENSE      parse.h        simplify.c    test-lexing.c\ncheck.c          compile.h         FAQ           linearize.c  pre-process.c  sort.c        test-linearize.c\ncompat           compile-i386.c    flow.c        linearize.h  ptrlist.c      storage.c     test-parsing.c\ncompat-cygwin.c  cse.c             flow.h        liveness.c   ptrlist.h      storage.h     test-sort.c\ncompat.h         evaluate.c        ident-list.h  Makefile     README         symbol.c      token.h\n$ \n"}]}