What is Git Add?

Git is a Distributed version control system to track the changes in the source code. One of the fundamental commands in Git is git add. Understanding what git add is and how to use it is important for anyone working with Git. This article will provide a comprehensive guide on 'git add', explaining its purpose, usage, and best practices.

What is Git Add?

git add is a command in Git that stages changes in your working directory for the next commit. It adds changes from the working directory to the staging area, also known as the index. The staging area is an important part of Git’s workflow as it allows you to prepare changes before committing them to the repository.

Why is Git Add Important?

git add serves several important functions:

  • Selective Staging: You can stage only specific changes or files, giving you control over what goes into your next commit.
  • Commit Preparation: It lets you review changes before they are committed, ensuring that only desired modifications are included.
  • Version Control: By staging changes, you can create logical and meaningful commits, making your project’s history easier to understand and maintain.

How do you add files to your Staging Area?

For this, you have to use the command git add <filename>. This will add a specific file i.e., you choose from your working tree to the staging area. If you want to add all the files to the staging area then use git add. The dot(.) operator will take all the files and add them to the staging area.

git add . : Staged new and modified files without deleting.
git add -a : Staged all files to the staging area.
git add -u : Staged modified and deleted files.

Let’s say you create a new folder inside the Git folder named SetUp and inside this SetUp folder, you create two files file1.txt and file2.txt. Now after git init, if you run git status then it is shown in red color means these files are untracked i.e., these are not in the staging area. Now if you run git add . , so the entire files in the working tree are added in the staging area and now it is showing in green color means that it is tracked.

Add files to staging area

To remove the file from the staging area the command used is: 

git rm --cached file-name

Note: Note that this will not delete the file, this will only remove the file from the staging area.

Refer to the below image.

What is Git Add?

You can see in the above image, we remove file1.txt from the staging area.