Sub topic 9
Creating Your First Commit
Prerequisites
This chapter assumes you are comfortable in a terminal and have Git installed. It also assumes you have run git init in a directory, set user.name and user.email, and know the three areas: working tree, staging area, and repository.
What git commit does
git commit takes whatever is in the staging area and records it as a new commit in the repository. Only staged changes are included. Edits you made but did not stage stay in the working tree and are left out.
The basic cycle
Run this in a repository where your identity is already configured:
echo "# Hello Git" > README.md
git status
git add README.md
git status
git commit -m "Add README"
git log --onelineEach step shows a different state:
- The first
git statuslistsREADME.mdunder "Untracked files". Git sees the file but does not manage it yet. - After
git add, the secondgit statuslists it under "Changes to be committed". The file is staged. git commitprints the branch name, a short commit hash, and(root-commit). That label appears only on the first commit in a repository.git log --onelineshows one line per commit: the short hash and the message.
Giving the commit a message
The -m option supplies the message on the command line. If you leave it out, Git opens your configured editor and uses what you save. Passing -m twice creates a subject line followed by a body paragraph:
git commit -m "Add usage notes to README" -m "Explain how to run the project locally."Keep the subject short and write it in the imperative mood, as in "Add README". Use the body to explain why the change was made.
Common errors
| Message | Cause | Fix |
|---|---|---|
| `Author identity unknown` | `user.name` and `user.email` are not set | Run `git config --global user.name "..."` and `git config --global user.email "..."`, then commit again |
| `nothing added to commit but untracked files present` | New files exist but none are staged | Run `git add <file>` |
| `no changes added to commit` | Tracked files were modified but not staged | Stage them with `git add`, then commit |
| `nothing to commit, working tree clean` | No changes exist since the last commit | Edit a file first, or accept that there is nothing to record |
If you are unsure which of these applies, run git status. It names the state you are in and usually suggests the next command.
Checking your result
After committing, git status should report a clean working tree. Use git log --oneline to confirm the commit exists, and git log without options to see the full hash, the author, the date, and the message.
Exercise
You run git add notes.txt, then add another line to notes.txt, then run git commit -m "Update notes". Does the commit include the new line? What does git status show afterward?
Show answer
The commit does not include the new line. It records the version staged by git add. Afterward, git status shows notes.txt under "Changes not staged for commit", because the working tree copy differs from the committed one. Run git add notes.txt and commit again to record the extra line.
Exercise
A teammate sees "Author identity unknown" on their first commit. Which two commands fix it, and why does Git require this information?
Show answer
git config --global user.name "Their Name" and git config --global user.email "[email protected]". Git stores the author name and email in every commit, so it refuses to create one until it knows them.