How to undo git add before commit?

There are two ways to undo git add prior to a commit. The first way allows you to remove all files that you added, while the second option allows you to remove one file that was added. We also include an example below to show when you would use this and what is happening.

Option 1: Remove All Added Files Before Commit

If you would like to undo all files added to the repository via git add before a commit has been made to the Git repository, there is a simple solution. Run the following command:

git reset

Option 2: Remove One Added File Before Commit

If you would like to undo one file added to the repository via git add prior to a commit, use the following command:

git reset <filename.txt>

An Example

Here’s an example to give you some more details. Let’s say that you have saved a README.html file and then erroneously added it to your local repository by running the following command:

git add .

If you run git status in the root directory, you’ll get the following in return:

On branch master
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   README.html

This means that the README.html file is staged and ready to be added to the permanent Git history, in other words, it is staged to be committed. However, if you run the following command, you can remove README.html from staging so that when you commit your changes, that README.html file will no longer be included.

git reset README.html

If you run git status again, you’ll see the following value returned. This means that the README.html file is in your list of Untracked files, and is no longer staged and ready to be committed to the repository’s history. Success!

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        README.html

Let’s say that you would like to undo all file additions to staging prior to a commit. In this case, you need to run the following git command:

git reset

Running git status will now show a list of all files that you added to staging prior to a git commit now under Untracked files. You could see something like the following:

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        README.html
        test.php
        User.php

And, that’s all there is to it!

Leave a Reply

Your email address will not be published. Required fields are marked *