Skip to content

Instantly share code, notes, and snippets.

@mekarpeles
Last active April 22, 2024 17:40
Show Gist options
  • Save mekarpeles/5fa3f2dd150bcbb8d136a2737863d086 to your computer and use it in GitHub Desktop.
Save mekarpeles/5fa3f2dd150bcbb8d136a2737863d086 to your computer and use it in GitHub Desktop.
Git aWalkthrough: The ins-and-outs of git

Git Walkthrough

This guide provides a walkthrough of a basic git workflow and attempts to describe what is happening under the hood as commands are run.

  1. We can create a new git repository using git init which creates a hidden directory called .git/
  • The init process fills .git/ with a special file called HEAD that contains the name of the branch or the commit we're on
  • If the contents of the HEAD file is the name of a branch, then we can look in .git/refs/heads/<branchname> e.g. .git/refs/heads/main to figure out the specific commit the branch is on: e.g. ba012fe642b796af4e5735a7e6c83e4e90f9e62b
  • When we first start out, HEAD is likely the main branch which maps to a nil (non-existant) commit
  1. We can stage files to be committed to a checkpoint by using the command: git add and then commit them using git commit -"message".
  2. When a commit is created, 3 types of data objects are created under the hood and stored within .git/objects:
  • blobs of data representing changes to our files (additions, deletions)
  • trees that show the state of the
  • a commit object that has a parent commit, a hash id, and a pointer to a tree object 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)
  1. After a commit is made, the ./git/refs directory is updated so the branch in HEAD points to the correct new commit
  2. git log can be used to see the chain of prior commits and git reflog may be used to see a history of recent actions that have been run using git.
  3. The git reset --soft command 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.
  1. git commit --ammend may be used to overwrite the last commit with new changes or a new message
  2. git reset --hard will 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 --hard reset to a past commit, your newer commits will no longer appear in git log and so it can be difficult to recorer.
  • The history in git reflog can be used to help you git reset back 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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment