← Back to Git Course | Chapter 2: Basic Commands | Lesson 7 of 18

Git add Command

Staging a Single File

To stage exactly one file, run git add <filename> with its exact path -- this copies that file's current contents into the staging area as a candidate for your next commit, without touching anything else you may have changed.

Example: Staging a Single File

bash
git add index.html

Staging Multiple Files

You're not limited to one file at a time -- git add file1.txt file2.txt stages several specific files in a single command, which is handy when you've touched a few related files but want to leave the rest untouched.

Example: Staging Multiple Files

bash
git add file1.txt file2.txt

Staging All Changes

When you've modified files scattered across many folders, staging each one individually is tedious -- git add . (stage everything in and below the current folder) or git add -A (stage every change in the whole repository) handles it in one shot.

Example: Staging All Changes

bash
git add .
git add -A

Staging by File Type

Wildcards let you stage by pattern instead of by name -- git add *.js stages every JavaScript file in the current directory, which is useful when you want to commit only code changes and deliberately leave config or data files unstaged.

Example: Staging by File Type

bash
git add *.js

Interactive Staging

Interactive staging (git add -p) walks you through a file's changes in small chunks called hunks, letting you accept or skip each one individually -- essential when a single file mixes an unrelated debug print with the real fix you want to commit.

Example: Interactive Staging

bash
git add -p

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.