Git Add

What is Staging?

Before saving changes permanently with a commit, files need to be placed in the Staging Area. The git add command does exactly this — it moves files from the Working Directory into the Staging Area.

The Staging Area acts as a preparation zone. It lets specific changes be grouped together before committing them as one logical unit. This is very useful when multiple files have been changed but only some of them should be included in the next commit.

Real-life analogy: Think of placing items into a shopping cart before checking out. The cart (staging area) holds what has been selected. Checkout (commit) finalizes the purchase. Items not in the cart are not part of the purchase.

Syntax

git add <filename>

Common git add Commands

Add a Single File

git add index.html

Only index.html is moved to the staging area.

Add Multiple Specific Files

git add index.html style.css

Both files are staged in one command.

Add All Files in the Current Directory

git add .

The dot (.) means "everything in the current folder and all its subfolders." This is the most commonly used form.

Add All Modified and New Files (Entire Repo)

git add -A

This stages all new files, modified files, and even deleted files — across the entire repository, not just the current folder.

Add All Modified Files (But Not New Untracked Files)

git add -u

This stages only modified and deleted files. Brand new untracked files are NOT staged.

Add Files by Pattern (Wildcard)

git add *.html

This stages all files with a .html extension in the current directory.

Practical Example

# Project folder has these files:
# index.html  (new file)
# style.css   (new file)
# script.js   (new file)

# Step 1: Check status
git status
# Output: Untracked files: index.html, style.css, script.js

# Step 2: Stage only index.html and style.css
git add index.html style.css

# Step 3: Check status again
git status
# Output:
# Changes to be committed:
#   new file: index.html
#   new file: style.css
# Untracked files:
#   script.js

# Step 4: Stage everything remaining
git add .

# Step 5: Check status
git status
# Output:
# Changes to be committed:
#   new file: index.html
#   new file: style.css
#   new file: script.js

Adding Parts of a File — Interactive Staging

Git can stage only specific parts (called "hunks") of a file, leaving other changes unstaged. This is useful when multiple independent changes are made to the same file and each should be committed separately.

git add -p index.html

Git will show each block of changes one at a time and ask what to do:

Stage this hunk [y,n,q,a,d,e,?]?
KeyAction
yStage this change
nSkip this change
qQuit and don't stage any more
aStage this and all remaining changes in file
sSplit this hunk into smaller pieces

Unstaging a File

If a file was accidentally staged and needs to be removed from the staging area (without losing the changes in the working directory):

git restore --staged index.html

On older versions of Git:

git reset HEAD index.html

Difference Between git add . and git add -A

CommandStages New FilesStages Modified FilesStages Deleted FilesScope
git add .YesYesYes (in newer Git)Current folder and below
git add -AYesYesYesEntire repository
git add -uNoYesYesEntire repository

What Happens to the File After git add?

After staging, the file's current content is saved as a snapshot in the staging area. If the file is edited again after running git add, those new changes are NOT automatically staged — git add would need to be run again.

# Example: Editing after staging
echo "Hello" > notes.txt
git add notes.txt          # Staged: "Hello"

echo "Goodbye" >> notes.txt   # Added more content

git status
# Output:
# Changes to be committed:
#   new file: notes.txt    ← the "Hello" version is staged
# Changes not staged for commit:
#   modified: notes.txt    ← the "Hello + Goodbye" version is NOT staged

Summary

git add moves files into the Staging Area in preparation for a commit. Specific files, all files, or even parts of a file can be staged. The most common command is git add . which stages everything in the current folder. Always verify what is staged by running git status after adding files.

Leave a Comment

Your email address will not be published. Required fields are marked *