In this article, we will learn how to use Git Add and Git Status commands in Git.
Total Views: 956
In this article, we will learn how to use Git Add and Git Status commands in Git. In a git repository, when a new file is added or an existing file modified or deleted in the working area, we need to push those changes to the staging area before moving to the local repository for the tracking. “git add” command is used to move the changes from the working area to the staging area. In the git repository, either file is tracked or untracked. A file is said to be tracked when the git can monitor the changes done in the file. Untracked files are ignored by the git. To track those files, we have to use the “git add” command
For demonstration, let's initialize a new Git repository with the use of the “git init” command as shown below.
Must check: What is the use of the git init command?
After the initialization of the git repository, add a new file (I have added index.html) in the working area.
Run the “git status” command in Git Bash to check which are untracked, which are tracked, modified, etc. “git status” is one of the most used git commands. Index.html file is showing in Untracked files with the red color.
To track the file, we need to run the git add command.
git add <FileName1> <FileName2> <FileName3> <FileName4> ….
or can use the “.” period symbol, which tells the git to track all files in the current directory and sub-directories within the current directory. Let’s add one file i.e., index.html file using the above syntax (“git add index.html”.
Again, check the status of the git repository, you can see that the index.html file is highlighted with green color and moved to the staging area. “git commit” command will push the files from the staging area to the local repository. We can also unstage the changes with “git rm” command.
Let’s edit the index.html file again and add a new file about.html.
On checking the status, we found that some of the changes of index.html are already pending for commit (highlighted with green color), the newly added file is untracked, for the new changes in index.html, the file is changed but not staged yet (highlighted with red color).
We can push all the pending changes from the working area to the staging area using the “git add .” or “git add -A” command.