The git add is a fundamental Git command used to add changes from the working directory to the staging area (also known as the index). This prepares the content for the next commit.
The command is used for both of the following purposes.
- Add new (untracked) file to Gitâs tracking (i.e. stage it)
- Stage changes made to existing (already tracked) files. When you run "git add <file>", Git takes the current contents of that file (whether new or changed) and stages it for the next commit.

- Stages Changes: Moves modified or new files to the staging area.
- Selective Staging: You can add individual files, multiple files, or all files at once.
- Prepares for Commit: Only staged files are committed when running git commit.
- Supports Partial Changes: With options, you can stage only certain parts of a file.
How to Use git add
1. Add a Single File
git add filename.txtStages filename.txt for the next commit.
2. Add Multiple Files
git add file1.txt file2.txtStages file1.txt and file2.txt.
3. Add All Files in the Directory
git add .Stages all modified, new, and deleted files in the current directory and its subdirectories.

4. Add Specific Patterns
git add *.txtStages all .txt files in the current directory.
5. Stage Parts of a File
git add -p filename.txtAllows you to interactively select hunks of changes to stage.
Common Options for git add
| Option | Description |
|---|---|
| . | Stage all changes in current directory |
| -A | Stage all changes including deletions |
| -u | Stage modified and deleted files, ignore untracked files |
| -p | Stage changes interactively by hunk |
| -n | Show what would be staged without actually staging |
Untracked Files Vs Tracked Files
Here are the differences between untracked and tracked files:
| Untracked Files | Tracked Files |
|---|---|
| Files that Git is not yet monitoring. | Files that Git is already monitoring (after being added once). |
| Created newly in the project but never staged or committed. | Already committed at least once in the past. |
| Need to be added using git add before they can be committed. | Changes must be staged again with git add before committing. |
| Example: A new file notes.txt you just created. | Example: Editing app.js that was already in the repo. |

