This chapter covers the core actions of the Git workflow, which move files through the three states and create a permanent history of your project.
1. Staging Changes (git add)
The git add command is used to move files (or specific changes within files) from the Working Directory to the Staging Area. This action prepares the files for the next snapshot.
| Scenario | Command Syntax | Description |
| Stage a Single File | git add index.html | Selects one file to include in the next commit. |
| Stage All Changes | git add . | Stages all modified and untracked files in the current directory and subdirectories. |
| Stage by Directory | git add src/css/ | Stages all changes within a specific folder. |
| Stage Specific Parts | git add -p file.js | Uses the patch option to stage only specific line-by-line changes, not the whole file. |
Key Role: After running
git add, the files are now in the Staging Area. Runninggit statuswill show them in green under “Changes to be committed.”
2. Committing Changes (git commit)
The git commit command takes the content that is currently in the Staging Area and records it as a permanent, versioned snapshot in the Local Repository. This is the action that solidifies your work into the project’s history.
Every commit creates a new reference point with essential metadata: the Author, Email, Timestamp, and the Commit Message.
Execution Examples
- Basic Commit (Opens Editor):This command executes the commit and opens your system’s default text editor (like Nano or Vim) to allow you to write a detailed, multi-line commit message.
git commit - Inline Commit Message:For quick commits with a short summary, use the -m flag. This is the most common method.
git commit -m "Add basic styling for navigation bar"
3. Best Practices for Commit Messages
A quality commit message is vital for collaboration and history review. It should explain why the change was made, not just what was changed.
- Subject Line: The first line should be a concise summary (50 characters or less) and written in the imperative mood (e.g., “Fix broken authentication,” not “Fixed broken authentication”).
- Body (Optional): Include a blank line followed by a more detailed explanation of the motivation, context, and any side effects of the change.
4. Bypassing the Stage (git commit -a)
The -a (all) flag is a shortcut that tells Git to automatically stage all currently tracked modified files before committing them. This saves you from running git add for every file that has been previously committed.
Caution: This flag only stages files that Git is already tracking. New, untracked files still require an explicit
git addbeforehand.
Execution
git commit -a -m "Quick fix: Update contact form endpoint"
