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.
Welcome back!
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:
- Working directory: the actual files on disk, the ones you open and edit.
- Staging area (also called the index): a holding zone for changes you've selected for the next commit.
- 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.
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.
- Working directory
Files you edit.
git statusshows 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.
- git add pancakes.md
- git status shows pancakes.md as modified
- git commit -m "Fix baking time for pancakes"
- Edit pancakes.md in a text editor
- 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 addthem. - Changes not staged for commit: tracked files that you've modified since the last commit but haven't staged. Git labels them
modified:(ordeleted:). - Changes to be committed: changes in the staging area that will go into the next commit, labeled
new file:,modified:ordeleted:.
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).
- Untracked files
- Changes not staged for commit
- Changes to be committed
- 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
| Aspect | git add file | git add . | git commit -a |
|---|---|---|---|
| New (untracked) files | Yes, the named one | Yes, all of them | No |
| Modified tracked files | Only the named one | All in this folder and below | All of them |
| Risk | Low: you choose | May sweep in junk or secrets | Forgets new files |
| Good for | Focused commits | Quick commits after checking status | Small 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?
Lesson complete!