Your first repository and commits
Turn a folder into a Git repository, check its state with git status, choose changes with git add, and save snapshots with git commit, understanding the working folder, staging area and history.
🎯 By the end of this lesson you will be able to:
- Create a repository with git init
- Read git status and move changes through the staging area with git add
- Save snapshots with git commit and write a clear commit message
Outline
What you'll learn and why it matters
This lesson is the heart of Git. You will use the same three commands, git status, git add and git commit, many times every working day. Once they feel natural, everything else in Git builds on them.
Create a repository
A repository (or repo) is a project folder that Git is tracking. Open a terminal in your my-website folder (from the HTML & CSS course, or any folder with a file in it) and run:
git initOutput
Initialized empty Git repository in /Users/asha/my-website/.git/
Git created a hidden folder called .git inside your project. That is where it stores the whole history. Never edit or delete it by hand: deleting .git deletes the history.
git status: what is going on?
git status is the command you will type most. It tells you what has changed and what to do next. Here Asha's folder already contains index.html:
git statusOutput
On branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) index.html nothing added to commit but untracked files present (use "git add" to track)
Untracked means Git can see the file but is not looking after it yet. Notice that Git even suggests the next command.
The three areas: working folder, staging area, history
| Area | What it holds | Moves on with |
|---|---|---|
| Working folder | Your files as they are right now, including unsaved experiments | git add |
| Staging area | The changes you have chosen for the next snapshot | git commit |
| History (the repository) | Every snapshot (commit) you have saved | - |
git add and git commit
git add index.html
git statusOutput
On branch main No commits yet Changes to be committed: (use "git rm --cached <file>..." to unstage) new file: index.html
The file is now staged: it will be in the next commit. Now save the snapshot. -m gives the commit message:
git commit -m "Add home page"Output
[main (root-commit) bbc1a1e] Add home page 1 file changed, 2 insertions(+) create mode 100644 index.html
| Part of the output | Meaning |
|---|---|
main | The branch the commit was saved on |
root-commit | The very first commit in this repository |
bbc1a1e | The start of the commit's unique id (a hash). Yours will be different |
1 file changed, 2 insertions(+) | A summary: one file, two lines added |
git statusOutput
On branch main nothing to commit, working tree clean
*Working tree clean* means every change is safely committed. That is the state to aim for before you stop work.
The everyday cycle
Change files, check, stage, commit. Here Asha adds a stylesheet and changes her page:
printf 'h1 { color: navy; }\n' > styles.css
printf '<p>I build accessible websites.</p>\n' >> index.html
git status
git add index.html styles.css
git commit -m "Add stylesheet and intro sentence"Output
On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: index.html Untracked files: (use "git add <file>..." to include in what will be committed) styles.css no changes added to commit (use "git add" and/or "git commit -a") [main cd8b247] Add stylesheet and intro sentence 2 files changed, 2 insertions(+) create mode 100644 styles.css
Git now says index.html is modified (it knows the file and sees a change) and styles.css is untracked (brand new). In the real terminal you type and see these one at a time; here they are shown together.
Writing good commit messages
| Weak message | Better message |
|---|---|
update | Add contact form to home page |
fixed stuff | Fix broken link to the menu page |
asdf | Make project cards stack on phones |
Write a short summary (about 50 characters or less) that completes the sentence *If applied, this commit will…*: Add, Fix, Remove, Update. Your future self, and your teammates, will read these messages to understand the project's history.
Practice
Guided exercise
Your first two commits
In a new folder, create a repository and a file called notes.txt with one line, and commit it with the message Add notes. Then add a second line to the file and commit again with Add second note.
mkdira folder,cdinto it and rungit init.- Create the file (in VS Code, or with
printf 'First note\n' > notes.txt). git add notes.txtthengit commit -m "Add notes".- Add a line, then add and commit again. Finish with
git status.
Show the solution
git init -q
printf 'First note\n' > notes.txt
git add notes.txt
git commit -m "Add notes"
printf 'Second note\n' >> notes.txt
git add notes.txt
git commit -m "Add second note"
git statusOutput
[main (root-commit) 13a4288] Add notes 1 file changed, 1 insertion(+) create mode 100644 notes.txt [main 83ad47b] Add second note 1 file changed, 1 insertion(+) On branch main nothing to commit, working tree clean
How to get there: Each change goes through add then commit. -q (quiet) on git init just hides its message. > creates or replaces a file and >> adds to the end of it. A clean status at the end proves both commits worked.
Your turn
Stage only one change
Asha changed both index.html and styles.css, but only the stylesheet change is finished. Commit just styles.css with the message Use navy headings, then show the status.
Show the solution
git add styles.css
git commit -m "Use navy headings"
git statusOutput
[main 3244a51] Use navy headings 1 file changed, 1 insertion(+), 1 deletion(-) On branch main Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: index.html no changes added to commit (use "git add" and/or "git commit -a")
How to get there: Only the staged file goes into the commit. index.html stays modified in the working folder, ready to be finished and committed later. A changed line counts as one deletion plus one insertion.
Common mistakes
Running git init in the wrong folder (such as your home folder)
You see: Git tracks thousands of unrelated files
Fix: Run pwd first. If it happened, delete the stray .git folder in that location (only there!).
Forgetting git add before committing
You see: nothing added to commit or no changes added to commit
Fix: Stage with git add, then commit.
Leaving out -m
You see: An editor opens asking for a message
Fix: Type a message, save and close the editor, or cancel and use -m "…".
Vague messages like update
You see: Nobody can tell what changed or why
Fix: Describe the change: *Add…*, *Fix…*, *Remove…*.
Recap
git initturns a folder into a repository; history lives in the hidden .git folder.git statusshows what changed and suggests what to do next.git addstages chosen changes;git commit -m "…"saves them as a snapshot.- Small, focused commits with clear messages make history useful.
Key terms
- Repository
- A project folder tracked by Git, including its full history.
- Commit
- A saved snapshot of staged changes, with a message, author and unique id.
- Staging area
- Where you collect the changes that will go into the next commit.
- Untracked
- A file Git can see but has never been asked to track.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. What does `git add index.html` do?
2. git status says a file is untracked. What does that mean?
3. Which is the best commit message?
4. You changed two files but staged only one, then committed. What happens to the other file's change?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.