status, add & commit
Three commands make up most of your daily Git use: git status to see what is going on, git add to choose what to save, and git commit to save it. You already know the idea from Working Tree, Index & Repository; this lesson is about using them well.
You will make the first commits in Priya's sweet-shop project, learn how to write messages your teammates will thank you for, stage only part of a file, and rename or delete files the Git way.
Your first commit
Priya creates index.html and style.css in her new repository and checks the status:
git statusOn branch main No commits yet Untracked files: (use "git add <file>..." to include in what will be committed) index.html style.css nothing added to commit but untracked files present (use "git add" to track)
Both files are untracked. She wants the home page in its own commit, so she stages only that file and commits it with a message given by -m:
git add index.html
git commit -m "Add home page"[main (root-commit) dae5ca8] Add home page 1 file changed, 11 insertions(+) create mode 100644 index.html
Read the summary line from left to right: the commit went onto branch main; it is the root commit (the first one, with no parent); its short ID is dae5ca8; and its message is "Add home page". Below that, Git counts the changed files and lines.
The stylesheet gets its own commit:
git add style.css
git commit -m "Add basic stylesheet"
git log --oneline[main 3a2c23c] Add basic stylesheet 1 file changed, 3 insertions(+) create mode 100644 style.css 3a2c23c (HEAD -> main) Add basic stylesheet dae5ca8 Add home page
Ways to stage files
You rarely add files one at a time. These are the common forms:
| Command | Stages |
|---|---|
git add index.html | One file |
git add css/ | Everything changed inside a folder |
git add . | All changes in the current folder and below, including new files |
git add -A | All changes in the whole repository, wherever you are |
git add -u | Changes to files Git already tracks, but no new files |
When Priya adds a menu page and an images folder, the short status shows a folder as one line. Add -uall to see every file inside:
git status -s
git status -s -uall
git add .
git status -s?? images/ ?? menu.html ?? images/notes.txt ?? menu.html A images/notes.txt A menu.html
?? means untracked and A means a new file has been added to the index. Before you run git add ., always look at the status: it is the easiest way to accidentally commit a password file or a huge folder. The next lesson, .gitignore, shows how to stop that for good.
Writing good commit messages
A commit message is a note to your future self and your team. Months later, git log is often the only explanation of why code looks the way it does. Good messages follow a few simple habits:
- Write the first line (the subject) in the imperative, like a command: "Add menu page", "Fix price rounding", not "Added" or "Fixing stuff".
- Keep the subject short, about 50 characters, with no full stop at the end.
- If the change needs explaining, leave a blank line and write a body that says why, wrapped at about 72 characters.
- Make each commit one logical change. "Add menu page and fix footer typo and update colours" is three commits.
For a message with a body, run git commit without -m. Git opens the editor you configured, and you write something like this, then save and close the file:
Show prices including GST on the menu
Customers were confused at the counter because the menu showed
prices before tax. All menu prices now include 5% GST, and the
footer says so.Staging part of a file
Sometimes one file holds two unrelated changes. Priya fixed the page title in menu.html and added a new sweet to the list. She wants two commits. git add -p (patch mode) shows each change, called a hunk, and asks what to do with it:
git add -p menu.htmldiff --git a/menu.html b/menu.html
index 6a285c9..4c8e519 100644
--- a/menu.html
+++ b/menu.html
@@ -2,13 +2,14 @@
<html lang="en">
<head>
<meta charset="utf-8">
- <title>Menu | Sharma Sweets</title>
+ <title>Menu - Sharma Sweets</title>
</head>
<body>
<h1>Our Menu</h1>
<ul>
<li>Kaju katli: ₹1,100 per kg</li>
<li>Gulab jamun: ₹480 per kg</li>
+ <li>Rasgulla: ₹420 per kg</li>
</ul>
</body>
</html>
(1/1) Stage this hunk [y,n,q,a,d,s,e,p,P,?]? s
Split into 2 hunks.The two changes were close together, so Git grouped them into one hunk. Answering s splits it. Git then shows each smaller hunk in turn; Priya answers n (no) for the title and y (yes) for the rasgulla line. Now the file is partly staged:
git status -s
git commit -m "Add rasgulla to the menu"
git commit -am "Use a hyphen in the menu page title"MM menu.html [main 17ac988] Add rasgulla to the menu 1 file changed, 1 insertion(+) [main daae9bf] Use a hyphen in the menu page title 1 file changed, 1 insertion(+), 1 deletion(-)
The second commit used -a, which stages every change to tracked files and commits in one step. -am "..." is a handy shortcut, but it never adds new untracked files, and it skips the chance to review what you are committing, so use it only when you know exactly what changed. Press ? at any patch prompt to see what all the letters mean.
Renaming and deleting files
If you rename or delete a tracked file with your file manager, Git sees one file deleted and possibly a new untracked file. The git mv and git rm commands do the change and stage it:
git mv style.css main.css
git rm images/notes.txt
git statusrm 'images/notes.txt' On branch main Changes to be committed: (use "git restore --staged <file>..." to unstage) deleted: images/notes.txt renamed: style.css -> main.css
Commit as usual. Git does not actually record renames; it notices that the content of the deleted file and the new file is the same and shows it as a rename. To stop tracking a file but keep it on your disk, use git rm --cached <file>.
Common mistakes
- Committing with a vague message such as "update", "changes" or "final". Describe what the commit does in a few words.
- Giant commits. A whole day of work in one commit is hard to review and impossible to undo partly. Commit each small step that works.
git add .from the wrong folder. It only stages the current folder and below. Usegit add -Aor run it from the project root.- Thinking
-aadds new files.git commit -aignores untracked files;git addthem first.
Key points
git statustells you which files are untracked, modified or staged; check it before every commit.git addstages files, folders or everything (.or-A);git commit -msaves the index with a message.- Good messages have a short imperative subject and, when needed, a body explaining why.
git add -pstages selected hunks, so one file can be split across several commits.git mvandgit rmrename and delete files and stage the change in one step.
Exercise
In a practice repository, create players.txt with three cricketers' names and scores.txt with their scores. Commit them in two separate commits with good messages. Then change one name and one score in different parts of players.txt and scores.txt, and use git add -p so that the next commit contains only the score change.
Show solution
git add players.txt
git commit -m "Add list of players"
git add scores.txt
git commit -m "Add first-match scores"
git add -p scores.txt
git status -s
git commit -m "Correct Rohan's score"In patch mode answer y for the score hunk. git status -s then shows M scores.txt (staged) and M players.txt (not staged), so the commit contains only the score change. The name change stays in the working tree for its own commit.
Quick Quiz
1. What does (root-commit) in the output of git commit mean?
The root commit starts the history; every later commit points back to a parent.
2. Which is the best commit subject?
Short, imperative, specific, and no full stop at the end.
3. Does git commit -am "msg" include a brand new file you just created?
New files must be added with git add at least once before -a will pick up their changes.
4. What does answering s in git add -p do?
Splitting lets you stage nearby changes separately.
Learn Git with a mentor
Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.
- Project-based learning
- Mentor code reviews
- Free counselling call
