Git can be thought of as a content manager for three trees or three collections of files.
Last commit snapshot, next parent.
HEAD is the pointer to the current branch reference - which in turn is a pointer to the last commit made on the current branch.
HEAD may be thought of as a snapshot of the last commit on the current branch.
To see the directory listing and SHA-1 checksums for each file in HEAD:
# Show content or type of HEAD object
git cat-file -p HEAD
# Recursively list the contents of the HEAD tree object
git ls-tree -r HEAD
The index or staging area is the proposed next commit snapshot.
The index consists of all file contents that were last checked out into the working directory, in the state they were in when last checked out.
When files are replaced with new versions, git commit
converts the index into a tree for a new commit. A new commit is created and the branch that HEAD points to is moved to the new commit.
The index is not technically a tree data structure - it is a flattened manifest - but can be thought of as a tree.
The working directory or working tree is a "sandbox" for working on files.
The HEAD and Index store content in the .git
directory - efficiently, but inconvenient in terms of access.
The working directory unpacks content into actual files, making them easily accessible for normal editing.
Changes can be tried out in the working directory, before being added to the staging area (index) and thereafter committed to history.
- Checkout a project, or start with a new project
- Work on files - in the working directory
- Stage files -
git add
files to the index - Commit files -
git commit
the contents of index, updating the master/HEAD pointer