First steps · lesson 4 of 8
Commands and ideas in this lesson
Agit initmake a folder a repository
Bgit statuswhat's changed, and where
Cgit addput a change in the staging area
Dgit commit -msave the staged changes as a commit

Your first repository

Last updated

Four commands make your first commit: git init, git status, git add and git commit. In this lesson you'll turn the kusina folder into a repository, save Lola Remy's adobo recipe in it, and meet the three areas every change passes through.

By the end of this lesson you can

  • Turn a folder into a repository with git init
  • Read git status and say which area each file is in
  • Stage a file with git add and save it with git commit -m
  • Say what a commit holds

git init and the hidden .git folder

git init turns the folder you're in into a repository. It changes nothing in your files. It adds one hidden folder, .git, where Git keeps the whole history and its settings. Everything Git knows about this project lives in that folder.

Go into the kusina folder and make it a repository. Then run ls -a, which lists hidden names too (they start with a dot), and git status, which tells you what Git sees.

Git sandbox
  1. Go into kusina
  2. Make it a repository with git init
  3. Find the hidden .git folder with ls -a
  4. Ask Git what it sees with git status
You are in your home folder, /home/ana. Git is installed and set up.

git status says the files are untracked: they're in the folder, but Git isn't keeping their history yet. You choose which files Git tracks.

The three areas

Every change in Git passes through three places. The strip across the top of every sandbox in this course shows them:

  1. Working folder: your files, as you see them in the folder and edit them.
  2. Staging area (Git also calls it the index): the changes you've chosen to go into the next commit.
  3. Repository: the commits, saved in .git for good.

git add copies a change from the working folder into the staging area. git commit saves everything in the staging area as a new commit. Watch one recipe make the trip.

$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	adobo.md
	sinigang.md

nothing added to commit but untracked files present (use "git add" to track)

Working folder

Staging area

nothing staged

Repository

0 commits on main

No commits yet.

1 of 4 A new repository with two recipe files. Both are in the working folder only. Git calls them untracked.

Why the middle step? Because you often change several things and want to save them as separate, tidy commits. The staging area is where you build the next commit. Lesson 1.5 uses it properly.

Your first commit

Save both recipes, one commit each. git commit -m "message" makes a commit with that message. The message says what the commit does: lesson 1.7 shows how to write good ones. For now, follow the pattern Add adobo recipe.

Git sandbox
  1. Stage adobo.md
  2. Commit it with a message like "Add adobo recipe"
  3. Stage and commit sinigang.md in a second commit
  4. Check with git status and git log

Working folder

Staging area

nothing staged

Repository

0 commits on main

You are in ~/kusina, a new repository with two recipes and no commits.

Commit graph

No commits yet.

git log lists the commits, newest first. When everything is committed, git status says nothing to commit, working tree clean: the three areas all match.

What a commit holds

A commit is not a list of changes. It's a snapshot: what every tracked file looked like at that moment. Along with the snapshot, it records:

  • the author: the name and email you set in lesson 1.3;
  • the time it was made;
  • the message;
  • its parent: the commit that came just before. The first commit has none, which is why Git printed (root-commit).

Each commit gets a name made from all of that: a 40-character hash such as 601c654bc163e7b14e71da58f3b877fe8f60a869. Git usually shows the first seven characters. Change anything, even one letter of the message, and the hash is completely different. Stage 8 explains how.

On your computer, the hashes will differ from the course's, because your commits have a different time.

Spot the trap: git init in your home folder

Spot the trapAna

Ana opens a terminal, which starts in her home folder, and types git init, meaning to start the recipes repository.

What happens if…

Always cd into the project folder before git init, and read the path Git prints back.

Check what you learned

You’ve edited adobo.md and run git add adobo.md, but not git commit. Where is the change?
What does git init change in your files?
Which is not part of a commit?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git