Open In App

What is Git Add?

Last Updated : 30 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Git is a distributed version control system designed to handle everything from small to very large projects with speed and efficiency. Among its many commands, `git add` is one of the most fundamental and essential. If you’re new to Git or need a refresher, this article will break down what `git add` is, how it works, and why it’s crucial for your development workflow.

What is Git Add?

The `git add` command is used to add changes in your working directory to the staging area. The staging area, also known as the index, is where you prepare a snapshot of your project’s current state before committing it to the repository.

Why Use git add?

  • Selective Staging: You might not want to commit all changes at once. git add allows you to select specific files or changes to include in your next commit.
  • Organized Commits: By staging specific changes, you can create more meaningful and organized commits. This is particularly useful when you’re working on multiple features or bug fixes simultaneously.
  • Error Reduction: Staging your changes allows you to review what will be committed, reducing the chance of accidental or unnecessary changes being included.

How to Use `git add`

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.


Next Article
Article Tags :

Similar Reads