How Git Works

Git Is Not What You Think


by scmGalaxy.com

About Me

DevOps@RajeshKumar.XYZ

We're going to talk about the internals of Git.

"Porcelain" Commands

  • git add
  • git commit
  • git push
  • git pull
  • git branch
  • git checkout
  • git merge
  • git rebase
  • ...

"Plumbing" Commands

  • git cat-file
  • git hash-object
  • git count-objects
  • ...

If you want to master Git, don’t worry about learning the commands. Instead, learn the model.

Git

Git Is..

…a Distributed Revision Control System

Git Is..

…a Revision Control System

Git Is..

…a Stupid Content Tracker

Git Is..

…a Persistent Map

Values and Keys

Any sequence of bytes

SHA1 hash

Values and Keys

"Apple Pie"

23991897e13e47ed0adb91a0082c31c82fe0cbe5

Every object in Git has its own SHA1.
So, what if they collide?

Winning the Jackpot



1 chance in 175.000.000

Winning the Jackpot



Chances of Two SHA1s Colliding



SHA1s are unique in the universe.

Git Is..

…a Persistent Map

Git Is..

…a Stupid Content Tracker

The Object Database

The Git Object Model

Git Objects

  • Blobs
  • Trees
  • Commits
  • Annotated Tags

What Git Really Is

Git Is..

…a Stupid Content Tracker

Branches Demystified

Git Is..

…a Stupid Content Tracker

Git Is..

…a Revision Control System

Git branches might not
be what you think they are.

The Master Branch

A branch is just a
reference to a commit.

A Second Branch

The Current Branch

HEAD is just a reference to a branch.

A New Commit

Checkout

Branching

Checkout

Merging

History and Content

Merge Commits

Git content management is simple.

Git (mostly) doesn't care about
your working directory.

Fast-forward

Checkout a Commit

Detached HEAD

The Git Object Model, Extended

Three Rules

  1. The current branch tracks new commits
  2. When you move to another commit, Git updates your working directory
  3. Unreachable objects are garbage collected

Rebasing Made Simple

Two Branches

Merge

Rebase

Checkout

Rebase (with Fast-forward)

After a Rebase

Git garbage-collects
unreachable objects.

Merge

Merges preserve history.

Rebase

Rebases refactor history.

When in doubt, just merge.

Git has two kinds of tags.

A tag is like a
branch that doesn't move.

Git Is..

…a Stupid Content Tracker

Git Is..

…a Revision Control System

Distributed Version Control

Git Is..

…a Distributed Revision Control System

Multiple Repos

Like a local branch, a remote branch
is just a reference to a commit.

"Apple Pie"

23991897e13e47ed0adb91a0082c31c82fe0cbe5

Synchronizing Repos

Synchronizing with a Remote

Adding a Commit

Git Push

Conflict!

Git Push-f

Git Fetch

Git Merge Origin/Master

Git Push

You "pull", then you "push".

Git Rebase Master

Conflict!

Solve the Conflict

Another User

Never rebase shared commits.

GitHub Features

Fork

Clone

Two Remotes

Commit

Push to Origin

Pull from Upstream

Push to Origin

Pull Requests

Git Is..

…a Persistent Map

Git Is..

…a Stupid Content Tracker

Git Is..

…a Revision Control System

Git Is..

…a Distributed Revision Control System

Questions