Git is Weird and Uses Magic


Help! I've fallen into a repo and can't get out!

A git repo is just a directory with a .git subdirectory, which is full of magic.

When you use Git, you literally cd into a repo to do your work.

You don't need a server, or even another directory somewhere else on disk. All you need is one directory with a .git subdirectory.

That's your repo.

 And you work inside of it.

That's weird.

Definition: Workspace

My workspace is everything in my directory, except for the magic .git subdirectory.

Note: For this article, I'm just going to assume all files are tracked. i.e. they've all been git added. Untracked files exist. But tracked and untracked and staged and unstaged are not nearly as weird as what I'm talking about today.

git init is pretty darn local

When I cd into a directory with a bunch of source files and type git init, git creates a .git subdirectory right there in my current directory. And now, drumroll....., my current directory is a Git repository, 100% fully as repository-full as any other Git repository.

Remember, if you're doing Git, you're in a repository. There's no escape.

The fundamental unit is, the Complete State of Everything.

Git stores commits. And a commit is the complete state of everything you care about in your project.

How does Git store all of that? It's magic. i.e. you can build a useful mental model without knowing it.

Moving on... Lot's of tutorials cover git add and git commit. They are not too weird.

The weird part is that every commit has:

  1. The complete state of everything you care about in your project, and
  2. A pointer to the previous commit.

So in all those pretty circles-and-arrows diagrams, each circle is the complete-state-of-everything, and every arrow is the pointer to the previous complete-state-of-everything.

checkout doesn't really go out

git checkout might have been called git timemachine. It changes the files in your workspace to match whatever commit you "check out".

It does not write new files and directories somewhere else.

If your editor updates automatically, it can be a bit surreal to watch the content of open files change before your very eyes.

Clone is deep. Clone is Everything.

Unlike checkout, git clone creates a new directory. The new directory has it's own magic .git folder and is, in fact, a complete new repository which contains the entire history or the original.

It's a Tree, Mostly

Each commit is a node with a parent. And what do we call a data structure where each node has one parent? It's a tree.

Except when you merge. Then it's not a tree.

Branches are Tiny and Don't Contain Stuff

A branch is just a named pointer to a single commit.

Really.

Just one little pointer.

Branches live in the repository. But commits don't know about them. (I think)

However, Git itself will do things with them. For example, Git will move a pointer forward when you make new commits. So you stay "on the branch".

But remember, we have a tree of commits, and a branch floats outside of the tree and points to exactly one commit.

And since a branch points to a commit, you can do things with branches that you would logically do with single commits, like a checkout that changes your workspace to match one.

Next, remotes and servers...

Comments

Popular posts from this blog

Callback, Promise, Observable, and Doughnuts

Learn grid and grid-template-areas FIRST!!!

The Day TDD Started Working for Me