Home » How to Undo add file in Git Before Commit

How to Undo add file in Git Before Commit

Last updated on June 12, 2020 by

Did you mistakenly add files using the git add commands and you haven’t yet run the git commit command? Do you want to ‘git undo add file’? Then you are at the right place. Let’s see how to do it.

Table of Contents
1. Git Undo Particular File
2. Git Undo Multiple Files

Revert Git Add Before Commit

There could be a different way to do it. Let’s good one.

01 Git Undo Particular File

If you want to undo a particular file then you can use git reset as shown below:

git reset [file-name]

Example:

Let’s suppose you have added a file 'list-view.php' using git add list-view.php command in Git and you want to revert this action.

  1. Let’s first run git status to see the modified/tracked/untracked files.
$ git status
On branch master
Your branch is up to date with 'origin/master'.

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

        new file:   list-view.php
  1. We can see that one file "list-view.php" ready to be committed but we don’t want to commit and revert this action. Let’s now run git reset filename command to revert it.
$ git reset list-view.php
  1. Let’s again run the git status to see what happened.
$ git status
On branch master
Your branch is up to date with 'origin/master'.

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

        list-view.php

nothing added to commit but untracked files present (use "git add" to track)

We can see that git status is now again showing list-view.php under the Untracked files: that means it is working correctly.

02 Git Undo Multiple Files

If you want to undo all added files then you can use the git reset command without any arguments as shown below.

git reset

Let’s see this via simple example.

Let’s suppose you have added a file 'list-view.php' & 'grid-view.php' using git add . command in Git and you want to revert this action.

  1. At very first run git status to see the modified/tracked/untracked files.
$ git status
On branch master
Your branch is up to date with 'origin/master'.

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

        new file:   grid-view.php
        new file:   list-view.php
  1. As we can see above that there are two grid-view.php & list-view.php files are ready to be committed but we don’t want to commit and revert this action. Let’s now run git resetcommand to revert it.
$ git reset
  1. Let’s again run the git status to verify.
$ git status
On branch master
Your branch is up to date with 'origin/master'.

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

        grid-view.php
        list-view.php

nothing added to commit but untracked files present (use "git add" to track)

We can clearly see that git status is now again showing both under the Untracked files: that means it is also working correctly.

Additionally read our guide, How to Undo Last Commit in Git

That’s it. We hope this article helped you to learn how to git undo add file before the commit.

Please let us know in the comments if everything worked as expected, your issues, or any questions. If you think this article saved your time & money, please do comment, share, like & subscribe. Thank you for reading this post 🙂 Keep Smiling! Happy Coding!

 
 

Leave a Comment