Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Git Tutorial
  • Git Exercises
  • Git Basic Commands
  • Git Cheat Sheet
  • Git Interview Questions
  • Git Bash
  • GitHub
  • Git Branch
  • Git Merge
  • Git WorkFlow
  • Git Hooks
  • Git LFS
  • Git Rebase
  • Git Cherry Pick
Open In App
Next Article:
Staging in Git
Next article icon

Staging in Git

Last Updated : 05 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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. 

Staging Changes

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.

Unstage a File

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 HunkAction Performed
yStage this hunk for the next commit
ndo not stage this hunk for the next commit
qquit; do not stage this hunk or any of the commits
astage this hunk and all later hunks in the file
ddo not stage this hunk or any of the later hunks in the file
emanually 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

Next Article
Staging in Git

A

ankitmahajan852
Improve
Article Tags :
  • Git

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
    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.
    5 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
    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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences