Git without fear · Chapter 2 of 28

Your first commit

By the end of this chapter you can put a folder under Git and save your work as commits: snapshots you can look back at and return to. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Day one
  • 5 tasks
  • Git
  • Free chapter

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.

Why you need this

Meera, the founder, keeps Tapri's menu in a plain folder: menu.txt, prices.csv and a README.md. Every few days a price changes and nobody knows who changed it, when, or what it was before. Last week cutting chai went from 25 to 20 for a day because Bunty typed over the file.

Today the only way to undo a change is to remember what the file said. Copying the folder to menu-final, menu-final-2 and menu-really-final does not tell you what changed between them either. Git keeps every saved version, who saved it and why, and shows exactly what changed.

The lesson

A repository

The terminal is already in Meera's folder. Ask Git what it sees:

git status
fatal: not a git repository (or any of the parent directories): .git

It is only a folder so far. git init makes it a repository:

Initialized empty Git repository in /workspace/chapters/02-first-commit/.git/

The hidden .git folder is the repository: every snapshot and everything Git knows lives there. Your files sit next to it, in the working tree.

Stage, then commit

Git never saves a file you did not ask for. Pressing straight on to a commit saves nothing:

git commit -m "Add the menu"
nothing added to commit but untracked files present (use "git add" to track)

First you put the files into the staging area, the list of what the next commit will contain. Then the commit takes a snapshot of exactly that list. Press Next to watch the three files travel:

git status after git add shows them under Changes to be committed. The commit prints its short id, 9a3c6d0 here (yours will differ: an id depends on who committed and when):

[main (root-commit) 9a3c6d0] Add the menu, prices and README
 3 files changed, 9 insertions(+)

The staging area has a second name, the index, which Git uses in its documentation and some messages. Interviewers like asking about it.

The loop

Now change a file. Two > signs add a line at the end (one > would replace the whole file):

echo "Bun maska 50" >> menu.txt
git diff
@@ -1,3 +1,4 @@
 Masala chai 40
 Cutting chai 25
 Elaichi chai 35
+Bun maska 50

git status calls the file modified: changed in the working tree, not staged, not saved. Stage it and commit again, and git log --oneline lists both snapshots, newest first:

That loop (change, git add, git commit) is what you will do all week. If the folder ever gets into a state you do not understand, lab reset builds it again from the start.

Your turn

Your five tasks are at the top, and the terminal prints them too (lab tasks shows them again). The last one is an interview warm-up about the staging area's other name.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    Make the folder a repository

    Make the chapter folder a Git repository, so Git can save its files.

    1. Run git status. It says there is no repository here yet.

    2. Run git init in the chapter folder.

    3. Run git status again. It now lists the three files as untracked.

    To pass: the chapter folder itself is a Git repository. A repository in a folder above it or below it does not count.

  2. Task 2

    Make the first commit with all three files

    Save the three files in the repository's first commit.

    1. Stage the files with git add: menu.txt, prices.csv and README.md.

    2. Run git status. The three files are under Changes to be committed.

    3. Commit them with git commit -m and a short message that says what is in it.

    To pass: the first commit has menu.txt, prices.csv and README.md in it. Any message is fine.

  3. Task 3

    Make a second commit with bun maska

    Add a new item to the menu and save it in a second commit.

    1. Add the line Bun maska 50 at the end of menu.txt. Keep the lines that are already there.

    2. Run git diff to see the new line.

    3. Stage menu.txt and commit again, with a message about bun maska.

    To pass: the latest commit's menu.txt has the line Bun maska 50, spelled exactly like that. The first commit must stay as it is, without bun maska.

  4. Task 4

    Leave nothing uncommitted

    Make sure every change is saved in a commit.

    1. Run git status.

    2. If it lists any file as modified, staged or untracked, stage it and commit it.

    3. Run git status again until it says there is nothing to commit.

    To pass: when you press Check, git status shows nothing to commit. Only answer.txt from task 5 may be left out.

  5. Task 5 · interview question

    Interview: the other name for the staging area

    Interview question: the staging area has a second name. Git uses it in its own documentation.

    1. Read the lesson again. It gives the second name after the first commit.

    2. Save that one word in answer.txt, in the chapter folder. echo with > writes text into a file.

    To pass: answer.txt has the staging area's other name.

What you will learn

  • A repository: the folder Git watches, and where it keeps the history.
  • The staging area: choosing what goes into the next snapshot.
  • A commit: the snapshot itself, with a message saying what it is.
  • The loop: change, stage, commit, and seeing each step with git status and git diff.

More in Day one

Build "Your first commit" yourself

Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

Free with a Google or GitHub sign-in, like the first 2 chapters of every lab.