Git Add



The git add command adds files content to the Staging Area (Index). It indicates to Git that you want to include modifications to a particular file in the next commit.

Every time we add a new file or update an old file in our project, we must forward updates to the staging area.

The git add command doesn't really affect the repository in any way; the changes are actually not recorded until you run git commit command.


Git Add a file

Git add command is a simple command. It adds files to the Index (Staging Area). We can add single or multiple files at once in the Index.

In the command below, we can see how it runs :

$ git add <path>

The command above adds a specific directory or file to the Staging Area.


Git Add All

Git supports the multiple add in one time using the command git add -A.

The command git add -A will add all files, including new, modified, and deleted files, including files in the current directory and in the higher directories that still relate to the same git repository.

The command will run as follows:

$ git add -A

Add All files but not in higher directories

The git add command supports an option that adds the entire directory recursively, but it will not include files outside this directory.

The command to add an entire directory will run as follows:

$ git add .

Note: The git add . will equal git add -A if you run the **git add . ** in the root folder of your repository.


Add all modified and deleted files

The git add command has an option that allows staging only the modified and deleted files. It will ignore the newly created file.

The command to stage just the modified and the deleted file will run as below :

$ git add -u 

Comparison between git add (-A, ., -u)

New files Modified files Deleted files Higher directories
git add -A Yes Yes Yes Yes
git add . Yes Yes Yes No
git add -u No Yes Yes Yes

Add files by wildcard

Git also allows adding multiple files at once using wildcard.

The command below will add all file that has a txt extension :

$ git add *.txt

Undo Added files

Git gives the possibility to undo a git add operation through git reset command.

The command below will undo an add operation:

$ git reset <filename>


ExpectoCode is optimized for learning. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy.
Copyright 2020-2021 by ExpectoCode. All Rights Reserved.