Staging, Committing, and Ignoring Changes
Explore the staging area (`git add`), committing changes with meaningful messages (`git commit`), and using `.gitignore` to exclude files from version control.
Staging Changes (git add
)
In Git, staging changes is a crucial step in the workflow. It allows you to selectively choose which modifications in your working directory you want to include in your next commit. The git add
command is the primary tool for achieving this.
Understanding the Staging Area
Think of the staging area (also called the index) as an intermediary holding area between your working directory (where you make your changes) and the Git repository's history. It's a place where you prepare your changes before permanently saving them with a commit.
Using git add
The git add
command moves changes from your working directory to the staging area. Here's how it works:
- Adding a specific file:
git add <filename>
For example:git add my_file.txt
- Adding all modified and new files in the current directory and its subdirectories:
git add .
- Adding all modified and new files in the entire repository:
git add -A
(orgit add --all
) - Adding only modified files (not new files):
git add -u
(orgit add --update
)
Example:
- You modify
index.html
. - You create a new file called
style.css
. - You run
git add index.html
. Now, only the changes toindex.html
are staged.style.css
is still untracked. - You run
git add style.css
. Now,style.css
is also staged. - You run
git commit -m "Added styling and updated index page"
. The changes to bothindex.html
andstyle.css
are now saved in the repository's history.
Learn how to use git add
to move changes from your working directory to the staging area, preparing them for a commit.
This is exactly what the previous sections explained. By using git add
with the appropriate options, you can carefully select which changes you want to include in your next commit. This allows you to create focused commits that represent logical units of work. Remember to always use git status
to check what changes are staged and what changes are still in your working directory before you commit.