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)
- Working directory (your folder): the files in your project folder, where you edit them. Git's messages sometimes call it the working tree.
- Staging area (also called the index): the list of changes that go into the next commit. It sits between the working directory and the repository.
- Repository: in this picture, the history of commits kept in the
.gitfolder.
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
git addprints nothing when it works..(a dot) means the folder your terminal is in, sogit add .picks every new or changed file in it.- Both files are now under
Changes to be committed, so they're in the staging area.
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
-mis an option for the commit message, which you write after it in quotes. The quotes are required when the message is more than one word.- In Git's answer,
mainis the branch (the line of work this commit is saved on).root-commitmeans this is the repo's first commit.4c908feis the first 7 characters of the hash, this commit's unique name. Your hash will differ, because Git also calculates it from your name and the time. 2 files changed, 2 insertions(+): 2 files changed, with 2 new lines in total.create mode 100644means the file is new to the repo (the number100644marks a normal file).- After the commit the staging area is empty again.
working tree cleanmeans every file in your working folder matches the last commit.
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?
- Git doesn't know who you are yet. Every commit records the name and email of the person who made it. So your first commit on a new computer can stop with
*** Please tell me who you are.Run these two commands once, with your own name and email:git config --global user.name "Your Name"andgit config --global user.email "[email protected]".--globalmeans the setting applies to every repository on your computer. On some computers Git doesn't stop: it guesses a name and email from your computer account and only shows a warning. That guess can be wrong, and it is still recorded in every commit. If you don't want your email to be visible on GitHub, use the noreply address (an address that hides your real email) that GitHub gives you under Settings, Emails. - Committing before
git add. The staging area is still empty, so there's nothing to commit. Git answersnothing added to commit but untracked files present(for new files) orno changes added to commit(for changed files). Rungit addfirst, then commit. - Forgetting the quotes. In
git commit -m first commit, onlyfirstis read as the message. Real Git refuses, because it takescommitfor a file name:error: pathspec 'commit' did not match any file(s) known to git. In the exercise, the terminal answers the same, and no commit is made. Writegit commit -m "first commit". - Editing again after
git add. The staging area keeps the file as it was when you rangit add. Later edits aren't included.git statusthen lists the file twice: underChanges to be committedand underChanges not staged for commit. Rungit addagain before you commit. - Two commits when one was asked for. The exercise asks for exactly 1 commit on
mainholding both files. Rungit add .first, then commit once.
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.