Tutorial by Examples

A git repository is an on-disk data structure which stores metadata for a set of files and directories. It lives in your project's .git/ folder. Every time you commit data to git, it gets stored here. Inversely, .git/ contains every single commit. It's basic structure is like this: .git/ obj...
git is fundamentally a key-value store. When you add data to git, it builds an object and uses the SHA-1 hash of the object's contents as a key. Therefore, any content in git can be looked up by it's hash: git cat-file -p 4bb6f98 There are 4 types of Object: blob tree commit tag
HEAD is a special ref. It always points to the current object. You can see where it's currently pointing by checking the .git/HEAD file. Normally, HEAD points to another ref: $cat .git/HEAD ref: refs/heads/mainline But it can also point directly to an object: $ cat .git/HEAD 4bb6f98a223abc...
A ref is essentially a pointer. It's a name that points to an object. For example, "master" --> 1a410e... They are stored in `.git/refs/heads/ in plain text files. $ cat .git/refs/heads/mainline 4bb6f98a223abc9345a0cef9200562333 This is commonly what are called branches. Howeve...
A commit is probably the object type most familiar to git users, as it's what they are used to creating with the git commit commands. However, the commit does not directly contain any changed files or data. Rather, it contains mostly metadata and pointers to other objects which contain the actual c...
A tree basically represents a folder in a traditional filesystem: nested containers for files or other folders. A tree contains: 0 or more blob objects 0 or more tree objects Just as you can use ls or dir to list the contents of a folder, you can list the contents of a tree object. $ git ca...
A blob contains arbitrary binary file contents. Commonly, it will be raw text such as source code or a blog article. But it could just as easily be the bytes of a PNG file or anything else. If you have the hash of a blob, you can look at it's contents. $ git cat-file -p d429810 package com.exampl...
The git commit command does a few things: Create blobs and trees to represent your project directory - stored in .git/objects Creates a new commit object with your author information, commit message, and the root tree from step 1 - also stored in .git/objects Updates the HEAD ref in .git/HEAD t...
When you run git checkout on a commit (specified by hash or ref) you're telling git to make your working directory look like how it did when the snapshot was taken. Update the files in the working directory to match the tree inside the commit Update HEAD to point to the specified hash or ref ...
Running git reset --hard moves refs to the specified hash/ref. Moving MyBranch to b8dc53: $ git checkout MyBranch # moves HEAD to MyBranch $ git reset --hard b8dc53 # makes MyBranch point to b8dc53
Running git checkout -b <refname> will create a new ref that points to the current commit. $ cat .git/head 1f324a $ git checkout -b TestBranch $ cat .git/refs/heads/TestBranch 1f324a

Page 1 of 1