Aller au contenu

Staging and committing files

The Git workflow

Recording a change in Git is a three-step workflow:

  1. Edit and save files on your computer, as usual, in the working directory (the project files you see and edit).
  2. Add the files to the staging area. The staging area holds the changes you have selected for the next record. It tracks what has been modified and is ready to be recorded.
  3. Commit the files. Git takes a snapshot of the staged files at that point in time and stores it in the repository. This snapshot is called a commit. Commits are what let you later compare files and revert them.
sequenceDiagram
    participant WD as Working directory
    participant SA as Staging area
    participant R as Repository (.git)
    Note over WD: edit and save inventory.csv
    WD->>SA: git add inventory.csv
    Note over SA: the change is selected<br/>for the next commit
    SA->>R: git commit -m "Add NAS"
    Note over R: new snapshot in the history

Staging versus committing

Think of sending a parcel:

Step Analogy Meaning
Staging (git add) Packing items into a box You prepare what will be sent. You can still add or remove items.
Committing (git commit) Sealing the box and shipping it with a label The content is recorded with a description. That snapshot is now part of the history.

The staging area lets you choose exactly what goes into each commit. For example, if you modified three files but only two belong to the same logical change, stage and commit those two, then commit the third one separately.

The life cycle of a file

From Git's point of view, every file in the working directory is in one of these states, and the commands on this page move it from one to the next:

stateDiagram-v2
    direction LR
    [*] --> Untracked: create the file
    Untracked --> Staged: git add
    Staged --> Unmodified: git commit
    Unmodified --> Modified: edit the file
    Modified --> Staged: git add
  • Untracked: the file exists, but Git does not record its versions yet.
  • Staged: the current content of the file is selected for the next commit.
  • Unmodified: the file is identical to its version in the last commit.
  • Modified: the file was edited since the last commit, and the change is not staged.

git status tells you which files are in which state.

Adding files to the staging area

Command Effect
git add README.md Stages a single file
git add . Stages all new and modified files in the current directory and its subdirectories
git add README.md   # stage one file
git add .           # stage everything under the current directory

The . is a path: it means "the current directory". If you run git add . from a subdirectory such as docs/, only the changes under docs/ are staged.

git add also turns an untracked file into a tracked one. After the first git add and commit, Git tracks the file and reports any later change to it.

$ git add README.md
$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
    new file:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    inventory.csv

README.md is now listed under Changes to be committed (staged), while inventory.csv is still untracked.

Common mistake: git add . stages everything

git add . is convenient but also stages files you may not want to record, such as temporary files or large outputs. Run git status before committing to check what is staged.

Making a commit

With both files staged, record them:

$ git add inventory.csv
$ git commit -m "Create README and inventory"
[main (root-commit) 2553c7d] Create README and inventory
 2 files changed, 4 insertions(+)
 create mode 100644 README.md
 create mode 100644 inventory.csv
Part of the command Role
git commit Records the staged changes as a new commit
-m "Create README and inventory" Provides the log message directly on the command line, without opening a text editor

Reading the output:

  • [main (root-commit) 2553c7d]: the branch (main) and the beginning of the new commit's identifier (its hash, explained in Viewing the version history). (root-commit) appears only for the very first commit of a repository, which has no parent.
  • 2 files changed, 4 insertions(+): a summary of the recorded changes (2 lines in each file).
  • create mode 100644 README.md: README.md is a new file in the repository. 100644 is the file mode Git records for a regular, non-executable file.

The next commits look the same, without (root-commit):

$ git commit -m "Add NAS and services list"
[main dd032b5] Add NAS and services list
 2 files changed, 5 insertions(+)
 create mode 100644 services.md

The log message

  • The log message is useful for reference: it explains why the change was made, for anyone reading the history later, including you.
  • Best practice: keep it short and concise, and describe the change. For example "Add NAS to inventory" instead of "update".
  • If you omit -m, Git opens a text editor so that you can write the message. Saving an empty message aborts the commit.

Only staged changes are committed

git commit records the content of the staging area, not the working directory. A file modified but not added with git add is not part of the commit. Run git status afterwards: it still lists those changes as Changes not staged for commit.

Configure your identity once

Every commit records an author name and email. If they are not configured, git commit fails with Author identity unknown and asks you to set them:

git config --global user.name "Your Name"
git config --global user.email "[email protected]"

In the labs, the identity is set without --global, so only the practice repository is affected.

Hands-on labs

Three labs, from guided to more autonomous. Each one has its own setup, so you can do them in any order and repeat them as often as you like. Type the commands rather than pasting them, and read every output before moving on: the goal is to build reflexes, not to reach the end.

Lab 1: your first commits

Objective: use the add/commit workflow to record a project in two separate commits, choosing what goes into each one.

Prerequisites and initial state: Git installed. The setup creates a fresh repository in ~/git-practice/staging with two untracked items.

Setup:

mkdir -p ~/git-practice/staging && cd ~/git-practice/staging
git init -b main
git config user.name "Practice User"
git config user.email "[email protected]"
printf '# Homelab\nTODO: describe the network layout.\n' > README.md
mkdir docs && echo "# Services" > docs/services.md

Tasks:

  1. Display the status: which files are untracked?
  2. Stage only README.md, check the status, then commit it with the message Add README.
  3. Stage everything that remains with a single command, and commit it with the message Add services documentation.
  4. Check that nothing is left to commit.

Expected result and verification:

  • After step 2, git status shows docs/ still untracked, and the first commit output contains (root-commit) and 1 file changed.
  • After step 4, git status prints nothing to commit, working tree clean.
  • git log --oneline (detailed on the next pages) lists exactly two commits: Add services documentation above Add README.
Solution
git status                                  # README.md and docs/ are untracked

git add README.md                           # stage a single file
git status                                  # README.md under "Changes to be committed", docs/ still untracked
git commit -m "Add README"                  # [main (root-commit) xxxxxxx] Add README

git add .                                   # stage everything under the current directory
git commit -m "Add services documentation"  # 1 file changed ... docs/services.md

git status                                  # nothing to commit, working tree clean
git log --oneline                           # two commits, newest first
  • Staging README.md alone keeps docs/ out of the first commit: the staging area defines the content of each commit.
  • git add . stages all remaining changes from the current directory down, which here means docs/services.md.
  • The commit hashes on your machine differ from any example: they depend on content, author, and date.

Clean up when you are done: rm -rf ~/git-practice/staging.

Lab 2: one change, one commit

Objective: make several unrelated edits in a row, then sort them into separate commits with the staging area.

Prerequisites and initial state: Git installed. The setup creates ~/git-practice/one-change with one commit containing three files. The README title has a typo.

Setup:

mkdir -p ~/git-practice/one-change && cd ~/git-practice/one-change
git init -q -b main
git config user.name "Practice User"
git config user.email "[email protected]"
printf '# Homleab\nNotes about my home servers.\n' > README.md
printf 'hostname,ip,role\npi-dns,192.168.1.10,dns\n' > inventory.csv
printf '# Services\n- pi-dns: Pi-hole\n' > services.md
git add . && git commit -q -m "Create homelab notes"

Tasks:

  1. Make three edits in a row, without staging anything:
    • fix the title of README.md (# Homelab);
    • add the line nas,192.168.1.20,storage to inventory.csv;
    • add the line - nas: Samba shares to services.md.
  2. Run git status. In which state is each file?
  3. The NAS lines belong together. Stage them, check the status, and commit with the message Add NAS.
  4. Before committing the remaining change, predict what the commit output will say, then commit it with the message Fix README title.
  5. Check that nothing is left to commit, and list the commits.

Expected result and verification:

  • Task 2: the three files are listed under Changes not staged for commit as modified.
  • Task 3: before the commit, inventory.csv and services.md are under Changes to be committed, and README.md is still under Changes not staged for commit. The commit prints 2 files changed, 2 insertions(+).
  • Task 4 prints 1 file changed, 1 insertion(+), 1 deletion(-): fixing a line means deleting the old one and adding the new one.
  • Task 5: nothing to commit, working tree clean, and git log --oneline shows Fix README title, Add NAS, Create homelab notes.
Solution
# 1. Three edits (or use a text editor)
sed -i 's/Homleab/Homelab/' README.md
echo "nas,192.168.1.20,storage" >> inventory.csv
echo "- nas: Samba shares" >> services.md

# 2. All three are "modified", none is staged
git status

# 3. First logical change
git add inventory.csv services.md
git status                        # 2 staged, README.md not staged
git commit -m "Add NAS"           # 2 files changed, 2 insertions(+)

# 4. Second logical change
git add README.md
git commit -m "Fix README title"  # 1 file changed, 1 insertion(+), 1 deletion(-)

# 5. Checks
git status                        # nothing to commit, working tree clean
git log --oneline
  • git add accepts several files at once.
  • With git add . in task 3, the typo fix would have been mixed into Add NAS. One commit per logical change keeps the history readable, and makes it possible to undo one change without the other.
  • A commit records lines: Git does not see a modified line, only one line removed and one line added.

Clean up when you are done: rm -rf ~/git-practice/one-change.

Lab 3: a moving target

Objective: understand that the staging area holds a snapshot of a file, taken when you run git add, and recognise the messages Git prints when nothing can be committed.

Prerequisites and initial state: Git installed. The setup creates ~/git-practice/moving with one committed file.

Setup:

mkdir -p ~/git-practice/moving && cd ~/git-practice/moving
git init -q -b main
git config user.name "Practice User"
git config user.email "[email protected]"
printf 'hostname,ip,role\npi-dns,192.168.1.10,dns\n' > inventory.csv
git add . && git commit -q -m "Create inventory"

Tasks:

  1. Add the line nas,192.168.1.20,storage to inventory.csv, and stage the file.
  2. Add a second line, web01,192.168.1.30,web, without staging it again. Run git status: why does inventory.csv appear twice?
  3. Commit with the message Add NAS. Run git status again: which line was committed, and which one was not?
  4. Try git commit -m "Add web server" right away. Read the message, then do what it takes to make this commit.
  5. Create a directory docs containing a file todo.md, and a file notes.txt at the root. From inside docs, run git add ., then commit with the message Add to-do list.
  6. Back at the root, run git commit -m "Add notes" without staging anything. Read the message.

Expected result and verification:

  • Task 2: inventory.csv is listed both under Changes to be committed (the NAS line) and under Changes not staged for commit (the web line).
  • Task 3: 1 file changed, 1 insertion(+), and inventory.csv is still modified afterwards.
  • Task 4 first prints no changes added to commit (use "git add" and/or "git commit -a").
  • Task 5 commits docs/todo.md only.
  • Task 6 prints nothing added to commit but untracked files present (use "git add" to track), and notes.txt is still untracked.
  • git log --oneline shows Add to-do list, Add web server, Add NAS, Create inventory.
Solution
# 1. Stage a first change
echo "nas,192.168.1.20,storage" >> inventory.csv
git add inventory.csv

# 2. Change the file again after staging it
echo "web01,192.168.1.30,web" >> inventory.csv
git status          # inventory.csv under "to be committed" AND under "not staged"

# 3. Only the staged snapshot is committed
git commit -m "Add NAS"
git status          # still modified: the web line was not part of the commit

# 4. Nothing staged, nothing to commit
git commit -m "Add web server"   # no changes added to commit ...
git add inventory.csv
git commit -m "Add web server"

# 5. "." is the current directory
mkdir docs
echo "# To do" > docs/todo.md
echo "draft" > notes.txt
cd docs
git add .
git commit -m "Add to-do list"   # create mode 100644 docs/todo.md
cd ..

# 6. Untracked files are never committed by accident
git commit -m "Add notes"        # nothing added to commit but untracked files present
git log --oneline
  • git add copies the file as it is at that moment into the staging area. Later edits stay in the working directory until you run git add again.
  • This is why a file can be "staged" and "modified" at the same time: the staging area and the working directory hold two different versions.
  • git add . from docs only covers docs, so notes.txt stays out of the commit.

Clean up when you are done: rm -rf ~/git-practice/moving.