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
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • jQuery
  • AngularJS
  • ReactJS
  • Next.js
  • React Native
  • NodeJS
  • Express.js
  • MongoDB
  • MERN Stack
  • PHP
  • WordPress
  • Bootstrap
  • Tailwind
  • CSS Frameworks
  • JS Frameworks
  • Web Development
Open In App
Next Article:
How to Undo 'git add' Before Commit?
Next article icon

How to Undo 'git add' Before Commit?

Last Updated : 27 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To undo a 'git add' before committing, you can use the 'git reset' command. This command unstages the changes that you previously added with 'git add', but it doesn't discard the changes from your working directory. This allows you to make further modifications to the files or decide later whether you want to include them in a future commit.

You have a Git repository named 'convert-klm-to-miles' with three files: 'index.css' , 'index.html', and 'index.js'. You accidentally staged index.html using git add, but you want to unstage it before committing you can use the following steps:

Steps to Undo 'git add' Before Commit?

Step 1: Navigate to your repository

Open your terminal or Git and navigate to the `convert-klm-to-miles` directory.

cd convert-klm-to-miles
Screenshot-2024-06-13-190323

Step 2: Check the status

This command check the current status of the working directory and staging area.

git status
Screenshot-2024-06-13-190735

If `git status` keeps showing "nothing to commit, working tree clean," ensure you have actually saved the changes to 'index.html' in your text editor.

Step 3: Stages the changes using `git add`

using the `git add` to stage the changes and then undoing the `git add` if any necessary.

git add index.html
Screenshot-2024-06-13-190851

Check the status again:

Screenshot-2024-06-13-190907

Step 4: Unstage the changes using `git reset`

Use this command line `git reset HEAD <file> ` is used to unstage changes that have been added to the staging area with `git add`.

git reset HEAD index.html
Screenshot-2024-06-13-191110

Again check the status:

Screenshot-2024-06-13-191139

Step 5: Stage the changes again

If you decide to stage `index.html` again. Means Again changes the Stages.

git add index.html
Screenshot-2024-06-13-191400

Step 6: Commit the changes

When you run git commit -m "Update index.html", Git takes all the changes that have been staged (usually done with git add) and packages them into a commit with the message "Update index.html". This commit is then added to the history of your repository.

git commit -m  "Update index.html"
Screenshot-2024-06-13-191405

Step 7: Push the changes to the remove repository

The command `git push origin master` uploads your local repository changes to the remote repository named `origin` and updates the `master` branch with these changes on the remote server.

git push origin master
Screenshot-2024-06-13-191628

Conclusion

To undo a mistaken 'git add' before committing, use 'git reset HEAD <file>'. This command removes the file from the staging area while keeping changes in your working directory intact. Once unstaged, you can decide whether to stage the file again or proceed without it in your next commit. Always verify changes with 'git status' and commit carefully to maintain a clean project history.


Next Article
How to Undo 'git add' Before Commit?

S

surya9c5sb
Improve
Article Tags :
  • Web Technologies
  • Git

Similar Reads

    How to Undo “git commit –amend"?
    Git is a powerful tool for version control, helping developers keep track of changes in their code and collaborate effectively. One handy feature is the git commit --amend command, which allows you to modify your most recent commit. In this article, we will see how to Undo “git commit –amend".Unders
    5 min read
    How to Undo a Commit in Git ?
    Git offers several ways to backtrack and correct mistakes, making it a powerful tool for developers. In this article, we will explore various methods to undo a commit in Git, ensuring that you can choose the best approach for your specific system.Below are the approaches to Undo a commit in Git:Tabl
    3 min read
    How to Undo Last Commit in Git?
    Sometimes, you might need to undo the last commit, whether it's due to a mistake, an incorrect commit message, or the necessity to rework the changes. This article will guide you through different methods to uncommit the last commit in Git, ensuring you can manage your repository effectively. Need t
    3 min read
    How to Revert a Git Commit?
    In software development, mistakes happen, and there will be times when you need to undo changes in your Git repository. Reverting a Git commit is a common task that can be handled in several ways, depending on your needs and the state of your project. This article will guide you through the differen
    4 min read
    How to Back Commit in Git?
    In this article, we are covering how to undo commits in Git. Sometimes, you might make mistakes or realize you need to revert changes you made in a previous commit. Luckily, Git offers a few ways to undo commits. Table of Content Approach 1: Using 'git revert'Approach 2: Using 'git reset' Approach 1
    4 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