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.
- Go into
kusina - Make it a repository with
git init - Find the hidden
.gitfolder withls -a - 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:
- Working folder: your files, as you see them in the folder and edit them.
- Staging area (Git also calls it the index): the changes you've chosen to go into the next commit.
- Repository: the commits, saved in
.gitfor 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.
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.
- Stage
adobo.md - Commit it with a message like
"Add adobo recipe" - Stage and commit
sinigang.mdin a second commit - Check with
git statusandgit 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
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
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.
- Git documentation: git-init: creating a repository and its .git folder
- Git documentation: git-status: untracked, staged and not-staged changes
- Git documentation: git-add: adding file contents to the index
- Git documentation: git-commit: -m and what a commit records
- Pro Git: Recording Changes to the Repository: the life cycle of a file's status