In this article we will try to understand each and every aspect which may help us to understand about Staging in Git. To keep a track of modifications or changes in the file we have to bring that changes to our staging area which we bring by using Staging. Below command and this process of adding these changes or modifications in an area to keep a track of it are called staging. While Hunk means a piece of changes. For example, we have written 4 lines of text in a file and modified 4 lines of text in that file that is known as a hunk which you can consider as a piece of change.
Following command we may use to add all the modified data while working on git:
git add .
Staging Changes
Here we will edit our file a.txt and after using the git status command we can see it is showing something in the green part that means that the file is staged but it is not committed and the part which is coming in red is there are some changes made in the file which are not being staged for example we may write hello in the file and then used “git add .” command to put it in the staging area but after that if we write “bhailogs” in the file and then if we are not adding that change in the staging area by using the git add command then we will see that in the red color which means that there are some changes which need to be tracked or staged. Now we will use various methods to add changes in a file to the staging area. If we want to stage all changes we will use the git add -A command or git add. where represents the current directory.Â

Unstage a File
Now if you want a file to be unstaged that is we don’t want that file to be in the staging area so what we can do is we can use the command git reset file_path to unstage a file. Now here we can see that we have added all the files in the staging area after applying the git status command we can see that all the files are in the staging area so now I want that b.txt should not be in the staging area so to remove b.txt from the staging area we have used the command git reset file_path but here we are in the path only so we don’t need to mention the entire path in file path because b.txt exists in our current directory so we will write git reset file_name to remove b.txt from the staging area.

Add changes by hunk
Here we have used the git add -p command and here we can see that there are no changes to be staged after that, we have edited the file a.txt. Â After editing
the file we again used the command git add -p command which will open an interactive prompt that will ask the user whether we want to
Stage this hunk along with various parameters.
Stages of Hunk |
Action Performed |
y |
Stage this hunk for the next commit |
n |
do not stage this hunk for the next commit |
q |
quit; do not stage this hunk or any of the commits |
a |
stage this hunk and all later hunks in the file |
d |
do not stage this hunk or any of the later hunks in the file |
e |
manually edit the current hunk |
? |
print hunk help |
which makes it easy to see changes that you want to stage or not.

Showing interactive prompt by using git add -p

Here I am entering y to add this hunk in the staging area
Interactive add Â
git add -i is the command which provides us an interactive interface along with various commands in that interface. So now let’s deep dive into this interactive add what is it and how its various commands provided in the interface works.

Showing interface provided by git add -i command
Here we can see the top half of the command output gives us the current state of the index broken up into staged and unstaged columns.
- a.txt has 1 line added and 0 lines removed. It is currently staged as the current status reports nothing in the unstaged column.
- b.txt has 0 lines added and 0 lines removed and has been staged. There are no further changes since it has been staged as indicated by the “nothing” line under the unstaged column.
The bottom half shows what you can do. Either enter a number (1-8) or a letter (s,u,r,a,p,d,q,h).
status shows output identical to the top part of the output above.

Using status command
Now to understand the working of the update command what we have done is have made some changes to a.txt and in staging whenever we modify something in your file or any folder we have to add those changes in the staging area. But we have not added those changes to the staging area. So here we will use the update command to add this file to the staging area. As you can see here on using the git status command we can see that we have modified a.txt but we have not added those changes into the staging area.

Using git status command to see the status of a.txt
Now let’s see the update commandÂ

Using update command
After using the update command now if we see the status of our project using git status so we can see the file a.txt has been added into the staging area.

Using the git status command to verify whether the changes are staged or not using the update command
revert basically reverts back information to the head. So when you get into the interactive mode using it add -I command and press 3 over there you get in the revert command so what happens now is it shows you files and for which file you want the revert to revert information back to head you can type that index number and then press enter. So what will happen it will mention an asterisk on that index position. and if you press enter after that it puts the respective file in the untracked files.

Using revert command
So you can also check nowÂ

with the git status command that the file b.txt is now in the untracked files.

Using git status command to verify that is b.txt is in the untracked files
add untracked  it allows you to add untracked files in the staging area.

Using add untracked command
Now we can see that b.txt is added into the staging area.

Using git status to see b.txt is in the staging area
patch it allows for one path to be selected out of output similar to status for further analysis
But here there are no changes to be staged therefore here it will show us no changes.

using patch command
diff displays what will be committed.

using diff command
quit exits the commandÂ

using quit command
help presents further help on using commands.

using help command
Staged Changes : To display the hunks that are staged for commit

Using git diff –cached
Stage a single file: Now if you want to stage a single file what you do is type in the command git add file_name it’s that easy and you will see that the respective file is being added. Now what  I have done is I have put my b.txt in the untracked files using the command git reset file_name here my file_name is b.txt and then I have used the command git add file_name to bring my b.txt in the staging area.

Using git add to stage a file
stage deleted files: To remove a file from git permanently use the -f flag in the command git rm -f file_name.
Â

Using git rm -f file_name to remove the file permanently
To delete the file from git without removing it from disk, use the –cached flag
So to understand this what I have done is created a file r.txt then added it into the staging area and then removed it using the –cached flag from git so what happened is it was removed from the staging area but did not get deleted from the disk and is put by git into the untracked files so now if I want that file r.txt to be added what I will do is use git add r.txt to add that file again into the staging area.

Using git rm –cached file_name

Using git status command to see whether r.txt is in untracked files or not

Using git add command to add r.txt file to the staging area
Similar Reads
Saving a File in Git
Git allows you to track changes, collaborate with others, and manage codebase efficiently. One of the fundamental tasks when working with Git is saving files, a process that involves several steps, including adding files to the staging area and committing them to the repository. In this article, we
2 min read
Undoing in Git
Undoing in Git means doing undo just like when we type something in any text editor and delete the same. After that, we think the text that we just deleted is needed, and then we use the undo operation to get back the old text. The same undoing in git is like doing undo in git. Common Scenarios for
6 min read
Using Patches in Git
Git allows developers to manage changes, collaborate with others, and maintain a history of their work. One of the lesser-known but highly useful features in Git is the ability to create and apply patches. This article will guide you through the process of using patches in Git, including creating pa
4 min read
What is "origin" in Git?
When working with Git, especially in collaboration with others, you will often encounter the term "origin." Understanding what "origin" means and how it functions within Gitâs workflow is important for effective source code management. In this article, we will walk you through everything you need to
4 min read
Install Git on Windows
Git is a powerful version control system used by developers worldwide. If you're looking to set up Git on your Windows machine, you have several options. This article will walk you through the most reliable and effective methods for installing Git on Windows, ensuring you stay up to date with the la
5 min read
Git Stash
When working on a project, there are times when you need to switch branches or temporarily set aside your changes without committing them. Git provides a feature called Git Stash, which allows developers to save uncommitted changes and restore them later. This is especially useful when working in a
4 min read
What is Git Init?
Git, a widely used version control system, allows developers to track changes in their code and collaborate efficiently. One of the first commands you will encounter when starting with Git is git init. This command is fundamental for creating a new Git repository, setting the stage for version contr
6 min read
Git Introduction
Git is a powerful and widely used version control system that helps developers track changes in their code, collaborate with others, and manage project history effectively. Whether you are a professional developer or just starting out, understanding Git is important for modern software development.
6 min read
Branching strategies In Git
Branches are independent lines of work, stemming from the original codebase. Developers create separate branches for independently working on features so that changes from other developers don't interfere with an individual's line of work. Developers can easily pull changes from different branches a
10 min read
Git - Status
Git is a powerful version control system that helps you to manage code changes and collaborate efficiently. One of the fundamental commands in Git is git status, which provides important information about the current state of your working directory and staging area. This article will explain the git
3 min read