Level 2

Init & config

What a Git repository is, how Git commands work, and how to create and configure your first repository.

Level 2 already? You are picking up steam fast. In this level you will find out what a Git repository actually is, learn how a Git command is shaped, and run your first two: one to create an empty repository, one to configure it with some basic settings.

Step 1 of 11
0 xp

A scrapbook for your code

But first things first: what even is a Git repository? Imagine that a Git repo is a scrapbook for your code.

A scrapbook is a homemade book where you keep special memories by sticking them onto the pages. Those memories could be anything: drawings, photos, ticket stubs, pressed flowers, leaves. We will call any item that can be stuck into a scrapbook a memento. Think of each memento as one of your project's code files at a given point in time.

The book and the table

Here is the rule that makes the whole picture work. Git knows about the mementos you have stuck onto a page. It knows nothing about the mementos strewn across your table.

The book is the repository. The table is the folder you work in. Files on the table are just files. They only become part of the history when you arrange them and stick them onto a page, and that arranging is what most Git commands are about.

What Git commands do

You interact with Git by running Git commands, which do a whole bunch of stuff:

  1. Sticking a memento onto a page.
  2. Inserting a new page into the scrapbook.
  3. Flipping to a page in the scrapbook to relive that treasured memory.

Git also has commands for fancier stuff:

  1. Combining mementos from several pages into a new page.
  2. Removing or reordering pages in the scrapbook.
  3. Sharing your scrapbook, or just parts of it, with friends.

With me so far? To run a Git command, you type git followed by the name of the action you want, and sometimes a few details after that. git init, git add hello.py, and so on. You will see each one before you are asked to use it.

First, a binder

Before you start running commands willy-nilly, you must create the scrapbook's binder. Without a binder, all your nostalgic mementos would fall straight to the floor.

Right now your project folder is empty. No binder, no pages, no mementos. That is exactly the right place to start.

Create the binder

git init (short for initialize) turns this empty folder into a repository. It creates a hidden folder called .git, and that folder is the whole scrapbook: pages, markers and settings all live inside it.

Type this into the terminal below
git init

There is not much to draw yet, because there is nothing in it. An empty binder with no pages and no mementos. An empty shell, brimming with potential, ready to hold your most prized possessions.

Your second command: git config

Before you start pasting things down like a maniac, you must configure some important settings. Specifically, your Git author information, which is linked to any change you make to the scrapbook.

Settings are changed with your second Git command, git config, which looks like this:

git config <setting> <value>

The pointy brackets mean "put your own value here". You never type them.

Set your author name

The user.name setting is your name, and it gets stamped on every page you add. It matters because you are not the only one who can add pages. Maybe you have an overly enthusiastic aunt who has to stick a photo of herself on every one.

Type this into the terminal below
git config user.name "Itsa Me"

The quotes are there because the name has a space in it. Itsa you!

safe writes 'user.name' to .git/config; no history or files change.

Set your email

Your Git email address is the one associated with any code changes you make. Hosting services such as GitHub match commits to accounts by it, so in real life use the address your account knows.

Type this into the terminal below
git config user.email itsa@me.com

To be honest there is no good scrapbook analogy for an email address. Some things are just email addresses.

safe writes 'user.email' to .git/config; no history or files change.

Name the main storyline

One last setup step: choosing a name for your repository's main line of work. That line is called a branch, and you will learn how branches work properly in Level 7. For now, set the default branch name to main, which is what most people use.

Type this into the terminal below
git config init.defaultBranch main

Three settings, three stamps on your binder: whose book it is, how to reach them, and what the main storyline is called.

safe writes 'init.defaultBranch' to .git/config; no history or files change.

One repository or all of them

The settings you just changed belong to this one repository. Add --global to a git config command and the setting applies to every repository on your computer instead:

git config --global user.name "Itsa Me"

Most people set their name and email globally once, and then override them in a repository only when they need a different identity there, say for a client's project.

Your turn: one repository, a different address

Every project on your machine uses your personal email, but this one is for a client and should carry your work address, itsa@work.example. Which command sets it for this repository only?

git-sim
Initial CommitDevlands

The graph appears here when the first command runs. The simulations are drawn by git-sim from a real repository.

The scrapbook · one picture for every Git idea

The scrapbook picture appears as the story is told.