Git Add & Commit

Files travel from the working directory (the folder where you edit files) to the staging area with git add, and then to the repository (the history of commits…

You've just made a commit, but git status still lists style.css as untracked. Wait, why wasn't it included? Relax, the file isn't lost. Git was simply never asked to save it.

A commit is a snapshot of your project at one moment, together with a message. But Git doesn't photograph every file right away. First you pick which files go in, through the staging area. It works like a group photo. Everyone is in the room, but only the people on the stage are in the photo. In Git, the people are the files in your project folder, the stage is the staging area, and the photo is the commit. You ask some files onto the stage with git add, then you take the photo with git commit. The photo gets a caption: the commit message.

The three places your files live

This picture shows the path of a file, from left to right:

Working directory  --git add-->  Staging area  --git commit-->  Repository
(your folder)                    (what you picked)              (the commit history)

git add: pick what goes in

In this example, git init has just run. The project folder holds index.html and style.css, and both are still untracked. There are three ways to pick files with git add:

git add index.html             # one file
git add index.html style.css   # several files at once
git add .                      # every new or changed file in this folder

The text after # is a comment: a note that the terminal ignores. The next example uses the last way, then checks the result with git status:

$ git add .
$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
	new file:   index.html
	new file:   style.css

If you had run only git add index.html, the next commit would hold only index.html. style.css stays in the folder, but it isn't saved, and git status still lists it as untracked. That is what happened at the start of this lesson.

git commit: save a snapshot

Now save what's in the staging area as a commit, then check again with git status:

$ git commit -m "first commit"
[main (root-commit) 4c908fe] first commit
 2 files changed, 2 insertions(+)
 create mode 100644 index.html
 create mode 100644 style.css
$ git status
On branch main
nothing to commit, working tree clean

Without -m, git commit opens a text editor (a program for typing text, usually Vim or nano) for you to write the message in. In Vim, press i to start typing. Then press Esc and type :wq to save and quit. In the exercise, the terminal imitates that editor: without -m the editor's text appears, and the next line you type becomes the message. Type :q! to leave without a message. Git then cancels the commit: Aborting commit due to empty commit message.

A good commit message

Commit messages are read by people, you included, months later. Keep it short, and say what changed:

Unclear Better
update add a contact form to the home page
fix fix the login button that can't be tapped on phones
asdf make the heading blue

Many teams use the command form ("add", "fix", "make"), as if finishing the sentence "this commit will ...".

⚠️ Why didn't the commit happen, or why is something missing?

TL;DR: Files travel from the working directory (the folder where you edit files) to the staging area with git add, and then to the repository (the history of commits in .git) with git commit. The staging area, also called the index, is the place in between: only what's in it goes into the commit. git add . picks every new or changed file in the folder your terminal is in. git commit -m "message" saves a snapshot (a picture) with the message in quotes. Without -m, Git opens a text editor (a program for typing) for the message. A commit without git add saves nothing. Write messages that say what changed. Check with git status until it says working tree clean, which means every file matches the last commit.

⚡ Pick first, then save

Write:

git add .
git commit -m "first commit"

Meaning: git add picks files and puts them in the staging area, where files wait before they go into a commit. Then git commit saves what's in the staging area as a snapshot (a picture), with the message written after the option -m. Like a group photo: the staging area is the stage, and only the files on the stage are in the photo (the commit). The dot . means every new or changed file in the folder your terminal is in.

⚡ Not added, not included

Guess first: your project folder holds index.html and style.css. You only run git add index.html, then commit. Where does style.css go?

It stays in the folder, it just isn't saved. git status still shows it as untracked (not recorded by Git).

Meaning: Only what's in the staging area goes into the commit. Check with git status until it says working tree clean (every file matches the last commit).

In the exercise, you must make exactly 1 commit on the branch main (your main line of work) holding both files: run git add . first, then commit once.

⚡ A commit without -m

Guess first: you type just git commit, without -m. What happens?

Git opens a text editor (a program for typing text, usually Vim or nano) for you to write the message in. In Vim, press i, type the message, then press Esc and type :wq to save and quit.

Meaning: After -m, write the commit message in quotes. Without quotes, in git commit -m first commit only first is read as the message. Git takes commit for a file name and refuses.

What you will learn