Git add Command with Step by Step with Example

git add command

The Git add command places new or changed files into the staging area so you can prepare them for the next commit. It does not save the work yet, but marks the files that you want Git to store later.

Understand the git add Command

Git add puts your file or folder into a staged state. Git will then know which files you want to save on the next commit. It does not store history itself but only marks changes.

Here is the command:

git add <file-or-path>

Replace <file-or-path> with the name of a file or a folder. Git then moves that file or folder to the staged area.

You can add one file with this form:

git add index.html

This command puts only the index.html file in the staged area so the next commit will include it.

You can add more than one file by typing each name:

git add file1.js file2.css

This command moves both file1.js and file2.css to the staged area so the next commit will include both files.

You can add every change in the current folder:

git add .

This command stages every new or changed file in the current folder so the next commit will include all of them.

The Difference Between git add and git commit

Git add only marks files for the next commit. Git commit creates a snapshot in the Git history. You can use git add many times before you run git commit. Git commit will then store all staged files at once. Git add prepares data but git commit records data. You use git add to collect files and git commit to save them.

Examples

Add a new HTML file:

git add about.html

This command marks the about.html file to be included in the next commit, so the change enters Git history after you run git commit.

Add every new and changed file:

git add .

This command places every changed or new file in the current folder into the staged area so the next commit records all these files.

Add files from a subfolder:

git add src/

This command stages all files inside the src folder so the next commit records that whole folder in one action.

Add only files with a name pattern:

git add *.js

This command stages every file that ends with .js so the next commit records all these JavaScript files.

Wrapping Up

You learned what git add does and how to use it with files, folders, and patterns.

Here is a quick recap:

  • git add marks files for commit but does not save them, git commit stores staged files in history, and you can use git add in many ways to control what goes into the next commit.

FAQs

What is Git add command used for?

The git add command stages changes in your project. It tells Git which files you want to include in the next commit. Example: git add file.txt

How do I add all files with Git add?

You can add all files at once with: git add . Alternative option: git add -A
  • git add . stages changes in current folder
  • git add -A stages all changes in repo

What is the difference between Git add and Git commit?

  1. git add tells Git which files to track.
  2. git commit saves those changes in history.
Example: git add index.html git commit -m "Add index file"

Can I undo a Git add command?

Yes, you can remove a file from staging with: git reset file.txt To remove all staged files: git reset

Similar Reads

Git config Command: A Complete Tutorial to Configure Git Settings

You can use the Git config command to set user data and control Git behavior. It saves preferences for projects…

Git Pull Force: Safely Overwrite Local Changes

If you've worked on any coding project, you would know this frustrating wall where Git simply refuses to pull the…

What is Version Control System? Software List Explained

One of the tools we need for software development is a Version Control System. It tracks code changes, making a…

How to Delete a Remote Branch in Git

When you work with teams, you often need to remove old remote branches. To delete a remote branch in Git,…

Git status Command with Examples

Git status command shows the state of files in your project. It shows files in the staged area, files not…

Git Pull: How to Keep Your Code in Sync

When you work on a project with friends, one writes the intro, one adds pictures, and others handle different parts.…

Git Push: A Step-by-Step to Syncing Your Local Repository

The purpose of the Git Push command is to upload local repository changes to a remote repository. Pushing your committed…

Git Delete Branch: Remove Local and Remote Git Branches

Git helps you manage code in different branches. You can delete a branch when it’s old or not being used.…

How to Switch Branches in Git: git switch vs checkout

The switch operation in Git will make it easier. Designed to simplify the process for developers, it's safer and more…

Install Git on Windows, Ubuntu & macOS: A Quick Guide

The moment you dive into programming or development, the first thing you are going to bump into is Git. It's…

Previous Article

JavaScript valueOf Function: How it Works with Examples

Next Article

PHP array_key_exists: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.