Git add and git commit: The Staging Area Explained
A commit is a saved snapshot of your project at one moment, together with a message, an author and a date. Commits are the units of history: you can compare them, go back to them and share them.
Three places for your files
Git thinks about files in three areas:
- The working directory: the files you see and edit.
- The staging area (also called the index): the set of changes you have chosen for the next commit.
- The repository: the commits stored in
.git.
The middle step is what makes Git different from a simple "save" button. You can edit five files and commit only the two that belong together, keeping each commit focused on one change.
git add
git add file.txt copies the current state of file.txt into the staging area. git add . stages every change in the current folder and below. If you edit a file again after adding it, the new edit is not staged until you add it again; git status will show the file as both staged and modified.
git commit
git commit -m "Add contact page" records everything in the staging area as a new commit. Git prints the branch, a short version of the commit's unique ID (a hash), the message and a summary of what changed. Without -m, Git opens a text editor for a longer message.
git init -q
echo "# Recipes" > README.md
echo "Pancakes: flour, milk, eggs" > pancakes.txt
git status --short
git add README.md
git status --short
git commit -m "Add README"
git add pancakes.txt
git commit -m "Add pancake recipe"
echo "Pinch of salt" >> pancakes.txt
git status --short
git commit -am "Add salt to pancakes"
git status --short Transcript (verified by our test suite in a fresh temp folder)
$ git init -q $ echo "# Recipes" > README.md $ echo "Pancakes: flour, milk, eggs" > pancakes.txt $ git status --short ?? README.md ?? pancakes.txt $ git add README.md $ git status --short A README.md ?? pancakes.txt $ git commit -m "Add README" [main (root-commit) d4dc383] Add README 1 file changed, 1 insertion(+) create mode 100644 README.md $ git add pancakes.txt $ git commit -m "Add pancake recipe" [main 569e6f0] Add pancake recipe 1 file changed, 1 insertion(+) create mode 100644 pancakes.txt $ echo "Pinch of salt" >> pancakes.txt $ git status --short M pancakes.txt $ git commit -am "Add salt to pancakes" [main 431fb9f] Add salt to pancakes 1 file changed, 1 insertion(+) $ git status --short
In the transcript, git status --short shows a compact two-column view: ?? means untracked, A in the first column means staged as new, and M in the second column means modified but not staged. Watch how one git add moves a file from the second state to the first.
The hashes in our transcripts are produced by the test run. Yours will be different because they depend on your name, email and the exact time.
Writing good messages
A commit message is a note to your future self and your teammates. Good messages:
- Start with a short summary in the imperative mood, under about 50 characters: "Fix login redirect", not "fixed stuff".
- Describe why in an optional body after a blank line, when the reason is not obvious from the change.
- Cover one logical change per commit, which is what the staging area makes possible.
Shortcut
git commit -am "message" stages every change to files Git already tracks and commits in one step. It skips new, untracked files, which still need git add.
Summary
- Working directory, staging area, repository: changes move left to right.
git addstages,git commit -mrecords a snapshot.- Keep commits focused and messages short and specific.
Quiz: Staging and commits