Nishaglobal Education logo

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.

Lesson 2 of 1135 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Create a repository
  3. git status: what is going on?
  4. The three areas: working folder, staging area, history
  5. git add and git commit
  6. The everyday cycle
  7. Writing good commit messages
  8. Practice
  9. Common mistakes
  10. Recap
  11. Quiz
  12. Mark lesson complete

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:

Terminal
git init

Output

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:

Terminal
git status

Output

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

Working folderyour files as they are nowgit addStaging areachanges chosen for the next commitgit commitHistorysaved commits (snapshots)git status shows what is in each area.
Files move through three areas, from top to bottom. You edit files in the working folder. git add copies chosen changes into the staging area. git commit saves everything staged as a permanent snapshot in the repository history.
AreaWhat it holdsMoves on with
Working folderYour files as they are right now, including unsaved experimentsgit add
Staging areaThe changes you have chosen for the next snapshotgit commit
History (the repository)Every snapshot (commit) you have saved-

git add and git commit

Terminal
git add index.html
git status

Output

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:

Terminal
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 outputMeaning
mainThe branch the commit was saved on
root-commitThe very first commit in this repository
bbc1a1eThe 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
Terminal
git status

Output

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:

Terminal
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 messageBetter message
updateAdd contact form to home page
fixed stuffFix broken link to the menu page
asdfMake 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.

  1. mkdir a folder, cd into it and run git init.
  2. Create the file (in VS Code, or with printf 'First note\n' > notes.txt).
  3. git add notes.txt then git commit -m "Add notes".
  4. Add a line, then add and commit again. Finish with git status.
Show the solution
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 status

Output

[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
Solution
git add styles.css
git commit -m "Use navy headings"
git status

Output

[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 init turns a folder into a repository; history lives in the hidden .git folder.
  • git status shows what changed and suggests what to do next.
  • git add stages 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. 1. What does `git add index.html` do?

  2. 2. git status says a file is untracked. What does that mean?

  3. 3. Which is the best commit message?

  4. 4. You changed two files but staged only one, then committed. What happens to the other file's change?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.