Skip to content
Related Articles

Related Articles

Git – Index

Improve Article
Save Article
  • Last Updated : 21 Mar, 2022
Improve Article
Save Article

Git Index may be defined as the staging area between the workspace and the repository. The major use of Git Index is to set up and combine all changes together before you commit them to your local repository. Let’s understand what this working workspace and local repo mean and functions before getting into deep with Git Index. Below is its pictorial representation.

 

 

Here we can see 4 different places where a file may reside, so let’s discuss them one by one.

 

A. Workspace: Whenever you work anything new on Git and it’s untracked then it remains in the workspace. All of these remain in your computer filesystem, and you can later add them to the staging area or index, directly commit it. 

 

B. Staging Area (.index): You can add your files from the workspace to the staging area. Before adding them you can check if, there exists any untracked file by using the command:

 

git status

 

Example: Here we have an untracked file named new_program.cpp, then using the above command the produced output would be:

 

 

We can add the following to our tracked files by using the command, we can use any of below commands depicted below  

 

git add -A
git add .

 

This command will add all the untracked changes from the code to the staging area. You can also specify particular changes using the command

 

git add [file]

 

 

The staged file means it is in the staging area, i.e., in the index. you can also unstaged your changes using the command:

 

git restore --staged <file>

 

C. Local Repository: The entire work of Git is based on this repository, it tracks the history and safeguards it. It also helps the user switch between the previous versions. You can commit your changes into your repository directly, using the command:

 

git commit -m [message]

 

D. Remote Repository: If the repository is also located remotely then it is known as the remote repository. A remote repository can be accessed from anywhere with correct credentials. You can push all your changes to your remote repository using the command:

 

git push

 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!