git add Command


git add Command

The git add command is used to stage changes in your working directory for the next commit. Staging involves selecting which changes you want to include in the commit, and git add allows you to specify files or directories to be included in this process.


What Does git add Do?

When you run git add, Git:

  1. Adds Changes to the Staging Area: It moves changes from your working directory to the staging area (also known as the index). The staging area holds the snapshot of changes that will be included in the next commit.
  2. Prepares Files for Commit: By staging files, you are telling Git which changes you want to commit. Only files that are staged will be included in the commit.

Basic Syntax

git add <file>
  • <file>: The path to the file or directory you want to stage. This can be a single file, multiple files, or a directory.

Examples

Adding a Single File

To stage a single file, use:

git add filename.txt

This command stages filename.txt, so it will be included in the next commit.

Adding Multiple Files

To stage multiple files, list them separated by spaces:

git add file1.txt file2.txt

This stages both file1.txt and file2.txt.

Adding All Changes

To stage all changes (including new files, modified files, and deletions), use:

git add .

or

git add -A
  • git add .: Stages all changes in the current directory and subdirectories.
  • git add -A: Stages all changes in the entire repository, including changes outside the current directory. This is equivalent to staging all new files, modified files, and deletions.

Adding Specific Types of Changes

  • Stage All Files with a Specific Extension:

    git add '*.txt'

    This stages all .txt files in the current directory and subdirectories.

  • Stage All Changes in a Directory:

    git add directory/

    This stages all changes in the specified directory and its subdirectories.


git add vs. git commit

  • git add: Moves changes to the staging area. It prepares changes for the next commit but does not actually create a commit.
  • git commit: Creates a commit from the changes that are currently staged. The commit includes a snapshot of the files in the staging area and a commit message describing the changes.

Example Workflow:

  1. Make Changes: Modify files or add new files.
  2. Stage Changes: Use git add to stage the changes you want to include in the commit.
    git add file1.txt file2.txt
  3. Commit Changes: Use git commit to create a new commit from the staged changes.
    git commit -m "Add file1 and file2 with updates"

Viewing Staged Changes

To see what changes are staged for the next commit, use:

git status

This command shows which files are staged, unstaged, or untracked.

To view the specific changes that have been staged, use:

git diff --cached

This displays the differences between the last commit and the staged changes.


Summary

  • Purpose: git add stages changes for the next commit by moving them from the working directory to the staging area.
  • Syntax: git add <file> (to add a specific file), git add . (to add all changes in the current directory), or git add -A (to add all changes in the entire repository).
  • Usage: Use git add to specify which files or changes you want to include in your next commit. It’s an essential step before running git commit to create a commit with the staged changes.

By staging changes with git add, you can control which modifications are included in each commit, helping to maintain a clean and organized commit history.