Lesson 2 of 5

Tracking Changes: Add and Commit

Starting a Repository

To let Git track a folder, move into it and run the initialisation command. This creates a hidden .git directory where Git stores all history.

git init

The Three Areas of Git

Git organises your work into three places. Understanding them is the key to using Git well.

  1. Working directory — your actual files as you edit them.
  2. Staging area — a holding zone for changes you plan to commit next.
  3. Repository — the permanent history of committed snapshots.

Checking Status

The git status command is your most-used command. It shows which files changed and which are staged.

git status

Staging Changes with git add

The git add command moves changes into the staging area. You choose exactly what goes into the next commit.

git add index.html        # stage one file
git add styles.css app.js # stage several files
git add .                 # stage every change in the folder

Saving a Snapshot with git commit

A commit permanently records the staged changes. Every commit needs a message describing what you did.

git commit -m "Add homepage layout and navigation"

The -m option lets you write the message right on the command line.

Writing Good Commit Messages

  • Write a short, clear summary of what changed and why.
  • Use the present tense, for example “Fix login bug”.
  • Commit small, related changes together rather than one giant commit.

Viewing History

The git log command lists past commits, newest first.

git log --oneline

The --oneline option shows each commit compactly on a single line.

Commit often. Small, frequent commits create a clear history and make it easy to find and undo the exact change that caused a problem.
Next
How did you find this lesson?

Discussion

0
To join the discussion, comment and react, log in or create an account.

No comments yet — be the first to leave one!