What is the git add command used for?
git add is used to add changes to the staging area (index), that is, to prepare files or parts of them for the next commit.
What git add does
After changing files:
text
Working directory → (git add) → Staging area → (git commit) → Repositorygit add:
- does not save changes permanently
- does not create a commit
- just marks changes that will go into the next commit
Main use cases
Add a single file
bash
git add index.jsAdd all changes
bash
git add .Adds:
- new files
- modified files
- deleted files
Add part of a file (interactively)
bash
git add -pAllows you to:
- choose specific changes (hunks)
- make logically clean commits
Add all files of a certain type
bash
git add "*.ts"Frequent mistakes
- Thinking that
git addimmediately makes a commit - Using
git add .without checkinggit status - Adding
.env,node_modules, build artifacts
Relation to git status
After git add:
bash
git statusShows:
- Changes to be committed
- Changes not staged for commit
Short version for an interview
git addadds changes from the working directory to the staging area, preparing them for a commit.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.