Find a course

Module 2: Everyday Git on Your ComputerLesson 3 of 1211 min

Your First Repository

Create a repository with git init and learn the three areas, working directory, staging area and commit history, that every change moves through.

Step 1 of 13Learn

From folder to repository

Lena keeps her family recipes as Markdown files in a folder called recipes. She's lost edits before, so she decides to track the folder with Git. In a terminal:

cd recipes

git init

Git replies Initialized empty Git repository in .../recipes/.git/. That's it: the folder is now a repository. Nothing about her files changed. Git simply created the hidden .git folder where it will store history.

She runs the command she'll use more than any other:

git status

Git lists her three files, pancakes.md, chili.md and bread.md, under Untracked files. Untracked means Git can see the files but isn't recording their history yet. Git never starts tracking files on its own; you always choose.

To start a repository from an existing project on GitHub instead, you'd use git clone, which you'll meet in the GitHub module. Either way, you end up with a folder plus a .git directory, and git status becomes your constant companion: run it before and after anything you're unsure about.

Step 2 of 13Learn

Three areas every change passes through

The single most useful idea in Git is that changes live in three places:

  1. Working directory: the actual files on disk, the ones you open and edit.
  2. Staging area (also called the index): a holding zone for changes you've selected for the next commit.
  3. Repository: the permanent history of commits stored in .git.

Changes move from left to right with two commands. git add copies the current state of a file from the working directory into the staging area. git commit takes everything in the staging area and saves it as a new snapshot in the repository.

Why have a middle step at all? Because you often change several things at once. Say Lena fixes a typo in pancakes.md and, in the same sitting, starts rewriting chili.md, which isn't finished. With staging, she can add only the pancake fix and commit it with a clear message, leaving the half-done chili changes in her working directory for later. Each commit stays focused on one idea, and history stays readable.

If you edit a file after staging it, the staging area still holds the earlier version. Run git add again to stage the newer changes.

The three areas of Git: working directory, staging area and repositoryWorkingdirectoryStaging area(index)Repository(commits)git addgit commitgit status shows what is in each area

Step 3 of 13Practice · Diagram

The three areas in action

Lena staged pancakes.md, then edited it again. Where does the newer edit sit?

Tap the shape that answers the question, then press Check.

The three areas in actionProcess diagram: Working directory → git add → Staging area → git commit → Repository (.git).Working directorygit addStaging areagit commitRepository (.git)
Working directory

Files you edit. git status shows red "not staged" changes here.

git add

Copies the current version of chosen files into the staging area.

Staging area

The draft of your next commit. Shown in green by git status.

git commit

Turns the staged snapshot into a permanent commit.

Repository (.git)

The history: every commit, safe and retrievable.

Step 4 of 13Practice · Put in order

Put the life of a change in order, from editing a file to having it saved in history.

Drag the items, or use the arrow buttons, to put them in order.

  1. git add pancakes.md
  2. git status shows pancakes.md as modified
  3. git commit -m "Fix baking time for pancakes"
  4. Edit pancakes.md in a text editor
  5. git status shows it under Changes to be committed

Step 5 of 13Learn

Reading git status

git status sorts every changed file into one of three groups:

  • Untracked files: new files Git has never recorded. They won't be included in commits until you git add them.
  • Changes not staged for commit: tracked files that you've modified since the last commit but haven't staged. Git labels them modified: (or deleted:).
  • Changes to be committed: changes in the staging area that will go into the next commit, labeled new file:, modified: or deleted:.

A file can appear in two groups at once. If you stage chili.md and then edit it again, status shows it under both "to be committed" (the staged version) and "not staged" (the newer edits).

When everything is committed, status says nothing to commit, working tree clean. That's the state you want before switching branches or pulling others' work.

Git's status output also includes hints, like (use "git restore <file>..." to discard changes in working directory). They're worth reading: Git is telling you the exact command for the most likely next step. For a compact view, git status -s prints one line per file, such as ?? notes.md for untracked and M bread.md for staged changes.

Step 6 of 13Practice · Match the pairs

Match each git status label to what it means.

Tap an answer, then tap the place where it belongs (or drag it there).

  1. Untracked files
  2. Changes not staged for commit
  3. Changes to be committed
  4. working tree clean

Step 7 of 13Learn

git add and git commit

Stage specific files by name:

git add pancakes.md

git add pancakes.md bread.md

Stage everything that changed in the current folder and below, including new files:

git add .

git add . is convenient, but it's how stray files end up in commits. Run git status first and make sure you want everything it lists. For finer control, git add -p walks through each changed section of a file and asks whether to stage it.

Then commit the staged changes with a message:

git commit -m "Add pancake, chili and bread recipes"

Git prints a summary like [main (root-commit) 3f9a1c2] Add pancake, chili and bread recipes followed by the number of files and lines changed. root-commit appears only on the very first commit; 3f9a1c2 is the short hash of the new commit.

If you leave out -m, Git opens your configured editor for a longer message. Write a summary line, a blank line, then any detail, save and close.

One shortcut to know: git commit -a -m "..." stages all changes to already tracked files and commits in one step. It never includes untracked files, which surprises people the first time.

Step 8 of 13Compare

Ways to stage

Aspectgit add filegit add .git commit -a
New (untracked) filesYes, the named oneYes, all of themNo
Modified tracked filesOnly the named oneAll in this folder and belowAll of them
RiskLow: you chooseMay sweep in junk or secretsForgets new files
Good forFocused commitsQuick commits after checking statusSmall edits to known files

Step 9 of 13Practice · What would you do?

What would you do?

Lena fixed a typo in pancakes.md (done) and is halfway through rewriting chili.md (not done). Both show as modified. She wants a commit containing only the finished typo fix. What should she run?

Step 10 of 13Note

Step 11 of 13Practice · True or false

Running git add notes.md permanently saves the file in the project history.

Step 12 of 13Practice · Fill in the blanks

Fill in the blanks

To record a new file index.html in history, run `git index.html and then git -m "Add home page"`.

Step 13 of 13Practice · Choose one

You ran git commit -a -m "Update site", but the new file about.html you created is missing from the commit. Why?