The git add command is used to add changes from the working directory to the staging area. Staging allows you to choose which changes should be included in the next commit.
git add does not create a commit.
It prepares selected changes for the next commit.
git add is a Git command used to stage changes for the next commit.
git add filename
After running this command, the selected changes are placed in the staging area.
Git allows you to choose exactly which changes should be included in a commit.
The git add command is used to make that selection.
git add index.html
The basic syntax is:
git add filename
For example:
git add index.html
Suppose you create a new file:
about.html
Git may show it as an untracked file. You can stage it with:
git add about.html
Use git status to see files that have not yet been staged.
git status
You may see:
Untracked files:
about.html
To add a single file:
git add about.html
Then check the status again:
git status
You can add multiple files in one command:
git add index.html style.css script.js
All specified files are added to the staging area.
To stage all changes in the current directory and its subdirectories, use:
git add .
This is commonly used when you want to stage all current changes.
Another commonly used command is:
git add -A
It stages additions, modifications and deletions across the working tree.
If you modify an existing tracked file, Git can show it as modified.
git status
To stage the modification:
git add index.html
After using git add, use:
git status
The staged files normally appear under:
Changes to be committed:
One important point is that git add does not create a commit.
git add index.html
After staging, you still need:
git commit -m "Add index page"
Modify Files
↓
git status
↓
git add
↓
Staging Area
↓
git commit
This is the basic workflow for recording changes with Git.
git add style.css
This stages the current changes to style.css.
git add script.js
This stages the current changes to script.js.
You can specify a directory:
git add css/
This stages changes under the specified directory according to Git's pathspec rules.
Git pathspecs can be used to select particular groups of files. For example:
git add '*.html'
This can be useful when you want to stage matching HTML files.
If a tracked file has been deleted, you can stage the deletion with:
git add deleted-file.html
You can also use:
git add -A
When a tracked file is renamed, staging the changes allows Git to record the resulting change.
git add old.html new.html
Git may later recognize the change as a rename when displaying history.
git status
git add .
git status
git commit -m "Update project"
Checking the status after staging helps you confirm what is prepared for the commit.
Suppose you changed three files but want to commit only two:
git add index.html style.css
The JavaScript file remains unstaged. This gives you more control over your commits.
A file can have both staged and unstaged changes. For example, you may stage a file and then make additional edits before committing.
git add index.html
# make another change to index.html
git status
Git can then show both staged and unstaged changes.
Git provides interactive staging with:
git add -p
This allows you to review changes in smaller sections and decide which changes should be staged.
If you accidentally stage a file, you can remove it from the staging area without deleting the working file.
git restore --staged index.html
The file remains in your working directory but is no longer staged.
mkdir website
cd website
git init
touch index.html
touch style.css
git add .
git status
This creates a repository and stages the new project files.
git init
git add .
git commit -m "Initial commit"
After git init, git add is commonly used
to prepare the initial project files for the first commit.
git add before committing.git add creates a commit.git add . without checking the changes.git add index.html
git add index.html style.css
git add .
git add -A
git add -p
git restore --staged index.html
These commands provide different ways to stage and manage changes.
mkdir student-project
cd student-project
git init
touch index.html
touch style.css
git status
git add index.html style.css
git status
git commit -m "Add project files"
git status
This example demonstrates creating a repository, adding files, checking their status and committing the staged changes.
The git add command moves selected changes from the working directory into the staging area.
git add filename
To stage all changes:
git add .
After staging, use git status to review the changes and
then use git commit to record them in the repository.
git add filename stages a specific file.git add . stages changes under the current directory.git add -A stages additions, modifications and deletions.git add -p allows interactive staging.git restore --staged.Question: Which command is used to stage changes for the next commit?