/    /  Git – Indexing/Staging area

Git Staging area

 

The staging area, also known as the index, is a component of a version control system such as Git. It serves as an intermediate step between the working directory and the repository, where changes to files can be prepared and reviewed before they are committed to the repository as a permanent record.

The git add command adds file contents to the index (staging area). It updates the current content of the working tree to staging. Every time we update a file, we have to push it there.

  • A core part of Git technology is the git add command. Normally, it adds one file at a time, but you can add multiple files at once.
  • There’s a snapshot of the working tree in the “index.” It’ll be forwarded to the next commit.
  • It is possible to run the git add command multiple times before making a commit. These all add operations can be subsumed under one commit. Git add doesn’t add the .gitignore file by default, but we can ignore them using this command.

Git add files

We can add one or more files at a time with the Git add command.

$ git add <File name>  

Despite being added to the git staging area, the above command cannot be shared on the version control system. A commit operation must be performed.

With the touch command, we’ve created a file in NewDirectory for our newly created repository.

$ touch newfile.txt  

Using the git status command, you can check whether it is untracked or not:

$ git status  

We can add the untracked files to our repository using the above command: newfile.txt. We have created a newfile.txt, so run the below command to add it.

$ git add newfile.txt  

Consider the below output:

Git Staging area

This shows newfile.txt has been added to our repository. Now we have to commit it.

Git Add All :

We If we want to add more than one file to Git, we have to run the add command repeatedly. Git gives us a unique option of adding all the available files at once with the add command. Run the add command with the -A option to stage all the files from the repository. ‘.’ instead of ‘.’ will stage all the files at once.

$ git add -A  

Or

$ git add .  

This adds all the files in the repository. Here’s an example:

You can either make four new files or copy it and add them all at once.

Git Staging area

In the above output shows all the files as untracked by Git. To track them all at once, run the following command:

$ git add -A  

Use the -A option to add all the files to the staging area.

Git Staging area

According to the output above, all files have been added and their status is staged.