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

Git add Command

git add is like choosing which photos to place in a frame before taking the picture. You pick exactly which changed files should go into the next snapshot.
Syntax
bash
git add file_name
git add file1 file2
git add .

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
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using git add file.txt and then editing the file again, expecting the newest edit to be included in the commit.
  2. Using git add . in the wrong directory, so files outside that folder are not staged.
  3. Adding files like node_modules or .env with git add . because there is no .gitignore.

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.