This guide provides a walkthrough of a basic git workflow and attempts to describe what is happening under the hood as commands are run.
- We can create a new git repository using
git initwhich creates a hidden directory called.git/
- The
initprocess fills.git/with a special file calledHEADthat contains the name of the branch or the commit we're on - If the contents of the
HEADfile is the name of a branch, then we can look in.git/refs/heads/<branchname>e.g..git/refs/heads/mainto figure out the specificcommitthe branch is on: e.g.ba012fe642b796af4e5735a7e6c83e4e90f9e62b - When we first start out,
HEADis likely themainbranch which maps to anil(non-existant) commit
- We can stage files to be committed to a checkpoint by using the command:
git addand then commit them usinggit commit -"message". - When a commit is created, 3 types of data objects are created under the hood and stored within
.git/objects:
blobsof data representing changes to our files (additions, deletions)treesthat show the state of the- a
commitobject that has aparentcommit, a hashid, and a pointer to atreeobject which describes the state of the project and contains a mapping of of what files and directories exist, where, and what changes were made to them (blobs)
- After a commit is made, the
./git/refsdirectory is updated so the branch inHEADpoints to the correct newcommit git logcan be used to see the chain of prior commits andgit reflogmay be used to see a history of recent actions that have been run using git.- The
git reset --softcommand can be used to reset git back-in-time to a previous a commit while retaining all the staged file changes that were involved in the original commit.
- This is useful if you checked in a file you didn't mean to and want to go back to a previous commit with all your progress, make changes, and then re-commit.
git commit --ammendmay be used to overwrite the last commit with new changes or a new messagegit reset --hardwill cleanly reset your branch back to a specific commit and not bring any of your staged files with it
- If you make a mistake and do a
--hardreset to a past commit, your newer commits will no longer appear ingit logand so it can be difficult to recorer. - The history in
git reflogcan be used to help yougit resetback to the right commit containing your precious files
The anatomy of a commit
Commit: <HASH ID>
- reference to its `PARENT` (previous commit)
- reference to its `TREE` database (phonebook) of files and folders related to this commit, e.g.:
blob1 readme.txt
blob2 file.txt
subdirectory (tree)