How to undo git add
How to undo git add
How to Undo a Git Add
To undo git add before a commit, run git reset or git reset to unstage all changes.
In older versions of Git, the commands were git reset HEAD and git reset HEAD respectively. This was changed in Git 1.8.2
You can read more about other commonly used Git actions in these helpful articles:
Here’s a bit more background information about Git
Understand the Three Sections of a Git Project
A Git project will have the following three main sections:
The Git directory (located in YOUR-PROJECT-PATH/.git/ ) is where Git stores everything it needs to accurately track the project. This includes metadata and an object database which includes compressed versions of the project files.
The working directory is where a user makes local changes to a project. The working directory pulls the project’s files from the Git directory’s object database and places them on the user’s local machine.
The staging area is a file (also called the “index”, “stage”, or “cache”) that stores information about what will go into your next commit. A commit is when you tell Git to save these staged changes. Git takes a snapshot of the files as they are and permanently stores that snapshot in the Git directory.
With three sections, there are three main states that a file can be in at any given time: committed, modified, or staged. You modify a file any time you make changes to it in your working directory. Next, it’s staged when you move it to the staging area. Finally, it’s committed after a commit.
Install Git
Configure the Git Environment
Git has a git config tool that allows you to customize your Git environment. You can change the way Git looks and functions by setting certain configuration variables. Run these commands from a command line interface on your machine (Terminal in Mac, Command Prompt or Powershell in Windows).
There are three levels of where these configuration variables are stored:
If there are settings that conflict with each other, the project-level configurations will override the user-level ones, and the user-level configurations will override the system-level ones.
Add Your Name and Email
Git includes the user name and email as part of the information in a commit. You’ll want to set this up under your user-level configuration file with these commands:
Change Your Text Editor
Add Color to Git Output
You can configure your shell to add color to Git output with this command:
Initialize Git in a Project
It’s important that the Git directory is installed in the project root folder. Git can track files in subfolders, but it won’t track files located in a parent folder relative to the Git directory.
Get Help in Git
If you forget how any command works in Git, you can access Git help from the command line several ways:
This displays the manual page for the command in your shell window. To navigate, scroll with the up and down arrow keys or use the following keyboard shortcuts:
Learn to code for free. freeCodeCamp’s open source curriculum has helped more than 40,000 people get jobs as developers. Get started
freeCodeCamp is a donor-supported tax-exempt 501(c)(3) nonprofit organization (United States Federal Tax Identification Number: 82-0779546)
Donations to freeCodeCamp go toward our education initiatives, and help pay for servers, services, and staff.
Git FAQ
Frequently asked questions around Git and Version Control.
How to Undo git add
Git does not automatically include changes in a commit: they have to be explicitly added to the next commit, with the git add command.
But sometimes you might change your mind and decide that a certain file should not be part of the next commit. In this short article, we’ll answer the question of how to undo adding a change to the staging area, after having used git add before.
The Git Cheat Sheet
Using git restore to Undo git add
This will remove the file from Git’s staging area, making sure it is NOT part of the next commit.
This will undo any modifications in this file since you last committed it. Please be careful with this command: undoing uncommitted local changes cannot be undone!
Undoing «git add» in Tower
In case you are using the Tower Git client, undoing a «git add» is as simple as unchecking the «Status» checkbox for the affected file:
Using git reset to Undo git add
The restore command is a quite recent addition to Git (in version 2.23). If you’re using an older Git version, you have to resort to the git reset command to achieve the same results:
Learn More
Get our popular Git Cheat Sheet for free!
You’ll find the most important commands on the front and helpful best practice tips on the back. Over 100,000 developers have downloaded it to make Git a little bit easier.
About Us
As the makers of Tower, the best Git client for Mac and Windows, we help over 100,000 users in companies like Apple, Google, Amazon, Twitter, and Ebay get the most out of Git.
Just like with Tower, our mission with this platform is to help people become better professionals.
That’s why we provide our guides, videos, and cheat sheets (about version control with Git and lots of other topics) for free.
Git Undo Add: A Guide
There’s plenty of reasons why you would want to remove an item from a Git commit. If you’ve not finished working on a file, it may not be ready to commit; you may still be testing it, or you may want to push your changes to that file as part of another commit.
Whatever the reason, you’ll maybe be wondering: how do I undo a git add? That’s a great question. In this short guide we’re going to walk you through undoing a git add using the git reset command. Let’s begin!
The Architecture of a Project
Before you can undo a git add, you’ve got to learn about how Git projects are structured.
Your local version of a project is stored in your working directory. This is your copy: you can change it as much as you would like. Changes you make to your local version of a repository are not reflected in the main version of a codebase until you commit them.
Adding items to the staging area is a reversible process. The changes you push to the staging area are not reflected in the main version of a repository until you commit them.
You can learn more about the git add file command line operation in our git add tutorial.
How to Undo a Git Add
Once a file has been committed, use git reset to remove it from a commit. This command removes a file from the staging area without making any changes to the file.
81% of participants stated they felt more confident about their tech job prospects after attending a bootcamp. Get matched to a bootcamp today.
Find Your Bootcamp Match
The average bootcamp grad spent less than six months in career transition, from starting a bootcamp to finding their first job.
Start your career switch today
Let’s remove a file called README.md from a commit:
Upon running this command, README.md is removed from the staging area. When you view the file you have removed from the staging area, you will notice no changes have been made. This is because the git reset command does not alter the contents of a file.
This command also works to remove all the files from a commit. Let’s say that you have just added every file in a repository to a Git commit and you didn’t mean to. You could reverse this using the following command:
When you use reset with no filenames, it will remove every item from the staging area.
The git reset command does not revert your repository to older versions or previous commits.
git rm: A Warning
The git rm command allows you to remove files from the staging area. This command has a dual purpose: it removes items from your local copy of a repository.
You should only use the git rm command if you want to remove items from both the staging area and your working directory. To remove a file from the staging area and your local copy of a project, you can use git rm like this:
Find Your Bootcamp Match
Select your interest
First name
Last name
Phone number
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.
This has removed README.md from the staging area. When we go to check the contents of this file, the file no longer exists. It’s been deleted from our system.
Conclusion
The git reset command allows you to remove a file or multiple files from a Git commit. If you want to remove an item from both the staging area and your local copy of a repository, you can use git rm. Use git rm with caution.
Now you’re ready to start undoing commits using Git commands like a professional!
How to Undo Git Add
At any stage of your work, you may like to undo something. You have that opportunity with Git. Here, we will show you the basic techniques of undoing an unwanted add in Git.
How to undo git add before commit
While working in Git, the files to be committed are gathered in the staging area. Now, suppose that you have added an unnecessary file to the staging area. Below, you will see how to remove that file easily. To remove only a single file from the staging area, you should run the command below:
To remove all the files from the staging area, run the following command:
As you see, git reset is used for undoing git add. Running only git reset without specifying the file name is especially useful when you are unable to list all the existing files one by one.
How to Use git amend to Add Files to the Commit
In other words, when you commit but then remember that you have not added some changes, you may act as follows:
The git commit Command
The git commit command can be related to the most important git commands. It is used for saving overall currently staged changes. You can create commits for capturing the current position of your project. Git always asks before changing the committed snapshots, hence they can be considered safe versions of the project. The git add command is used before git commit for making changes to the project which is going to be stocked in a commit.
The git reset Command
Whenever you need to undo changes, the git reset command will come to help you. It runs on the so-called “ The Three Trees of Git”. It involves the commit history ( HEAD ), the working directory and the staging index. This tool is useful when you have not pushed your commit to the remote repository yet.
How to Undo Git Add Command
The Git Add command is used to add files to the staging area or the index. Files are added to the staging area so that they are included in the next commit. But sometimes we may end up adding unwanted files to the staging area and don’t want these files to be a part of the next commit. Git provides us with a few different commands to undo a Git Add command. Let’s learn how to do this.
Undo Git Add
Two different commands can be used to undo the effects of Git Add. We have the Git Reset command, and we also have the newer Git Restore command. Let’s learn more about these commands.
Undo Git Add using Git Reset
Consider the following examples to understand the working of the Git Reset command when used to undo the Git Add command.
Suppose, we have three files added to the Staging Area.
Let’s first remove just one of these files.
and then check the status.
Now, let’s unstage all the files.
Undo Git Add using Git Restore
The Git Restore command is fairly new and was added in version 2.23 of Git. To undo the effects of a Git Add command on a certain file i.e. to remove a file from the staging area, use the —staged option with the command.
The following images illustrate the working of the Git Reset command.
We initially have three files in our staging area.
Let’s restore one of them and check the status of the repository.
Now check the status again.