Lesson 12 of 60 – Git Add
20%

Git Add

The git add command is used to add changes from the working directory to the staging area. Staging allows you to choose which changes should be included in the next commit.

Note: git add does not create a commit. It prepares selected changes for the next commit.

1. What is git add?

git add is a Git command used to stage changes for the next commit.

git add filename

After running this command, the selected changes are placed in the staging area.

2. Why Do We Need git add?

Git allows you to choose exactly which changes should be included in a commit.

The git add command is used to make that selection.

git add index.html

3. Basic Syntax

The basic syntax is:

git add filename

For example:

git add index.html

4. Adding a New File

Suppose you create a new file:

about.html

Git may show it as an untracked file. You can stage it with:

git add about.html

5. Checking the File Before Adding

Use git status to see files that have not yet been staged.

git status

You may see:

Untracked files:
  about.html

6. Adding One File

To add a single file:

git add about.html

Then check the status again:

git status

7. Adding Multiple Files

You can add multiple files in one command:

git add index.html style.css script.js

All specified files are added to the staging area.

8. Adding All Changes

To stage all changes in the current directory and its subdirectories, use:

git add .

This is commonly used when you want to stage all current changes.

9. Adding All Changes with git add -A

Another commonly used command is:

git add -A

It stages additions, modifications and deletions across the working tree.

10. Adding a Modified File

If you modify an existing tracked file, Git can show it as modified.

git status

To stage the modification:

git add index.html

11. Checking Staged Changes

After using git add, use:

git status

The staged files normally appear under:

Changes to be committed:

12. git add Does Not Commit

One important point is that git add does not create a commit.

git add index.html

After staging, you still need:

git commit -m "Add index page"

13. Git Add Workflow

Modify Files
     ↓
git status
     ↓
git add
     ↓
Staging Area
     ↓
git commit

This is the basic workflow for recording changes with Git.

14. Adding a CSS File

git add style.css

This stages the current changes to style.css.

15. Adding a JavaScript File

git add script.js

This stages the current changes to script.js.

16. Adding Files from a Folder

You can specify a directory:

git add css/

This stages changes under the specified directory according to Git's pathspec rules.

17. Adding a Specific Extension

Git pathspecs can be used to select particular groups of files. For example:

git add '*.html'

This can be useful when you want to stage matching HTML files.

18. Adding Deleted Files

If a tracked file has been deleted, you can stage the deletion with:

git add deleted-file.html

You can also use:

git add -A

19. Adding Renamed Files

When a tracked file is renamed, staging the changes allows Git to record the resulting change.

git add old.html new.html

Git may later recognize the change as a rename when displaying history.

20. Adding Changes Before Commit

git status
git add .
git status
git commit -m "Update project"

Checking the status after staging helps you confirm what is prepared for the commit.

21. Staging Only Selected Files

Suppose you changed three files but want to commit only two:

git add index.html style.css

The JavaScript file remains unstaged. This gives you more control over your commits.

22. Staging and Unstaged Changes

A file can have both staged and unstaged changes. For example, you may stage a file and then make additional edits before committing.

git add index.html
# make another change to index.html
git status

Git can then show both staged and unstaged changes.

23. Interactive Staging

Git provides interactive staging with:

git add -p

This allows you to review changes in smaller sections and decide which changes should be staged.

24. Removing a File from Staging

If you accidentally stage a file, you can remove it from the staging area without deleting the working file.

git restore --staged index.html

The file remains in your working directory but is no longer staged.

25. Git Add with a New Project

mkdir website
cd website

git init

touch index.html
touch style.css

git add .

git status

This creates a repository and stages the new project files.

26. Git Add Before the First Commit

git init

git add .

git commit -m "Initial commit"

After git init, git add is commonly used to prepare the initial project files for the first commit.

27. Common git add Mistakes

  • Forgetting to run git add before committing.
  • Assuming git add creates a commit.
  • Staging files that should not be committed.
  • Using git add . without checking the changes.
  • Forgetting that a file may contain both staged and unstaged changes.
  • Running Git commands in the wrong project directory.

28. Useful git add Commands

git add index.html
git add index.html style.css
git add .
git add -A
git add -p
git restore --staged index.html

These commands provide different ways to stage and manage changes.

29. Complete Git Add Example

mkdir student-project
cd student-project

git init

touch index.html
touch style.css

git status

git add index.html style.css

git status

git commit -m "Add project files"

git status

This example demonstrates creating a repository, adding files, checking their status and committing the staged changes.

30. Summary of git add

The git add command moves selected changes from the working directory into the staging area.

git add filename

To stage all changes:

git add .

After staging, use git status to review the changes and then use git commit to record them in the repository.

📌 Key Points

  • git add stages changes.
  • Staged changes are prepared for the next commit.
  • git add filename stages a specific file.
  • git add . stages changes under the current directory.
  • git add -A stages additions, modifications and deletions.
  • git add -p allows interactive staging.
  • git add does not create a commit.
  • Use git status to check staged changes.
  • Use git commit to record staged changes.
  • You can remove a file from staging with git restore --staged.

🧠 Quick Quiz

Question: Which command is used to stage changes for the next commit?