Skip to main content

Using Git

Undo Git Add and Remove Files from Staging

Accidentally add a file that was not supposed to be added? If you have not made the commit yet, you can undo the git add and remove the file from staging.

There are times when the files you added using the git add command were irrelevant and now you want to undo the git add.

You can totally do that. Here are two ways to undo the git add changes:

  1. Using the git reset command
  2. Using the git rm command

For the explanation, I added two files using the git add command: Hello.txt and LHB.txt:

Now, I'll show you how I'll remove them from the git add. Needless to say that you have not committed the changes yet.

Undo git add using the git reset command

🚧
Avoid this method if it's a newly initialized Git project that has no commits yet.

The git reset is the easiest way to undo git add as it does not require any complex flags and all you have to do is append the file or path to the git reset command:

git reset <file/path>

For example, here, I want to unstage the Hello.txt and LHB.txt file so I will be using the following:

git reset Hello.txt LHB.txt
Use the git reset command to undo git add in Linux

Alternatively, you can also use wildcard to unstage multiple files having the same file extension:

git reset *filetype

Let's say I want to unstage every file ending with .txt, then I will use the following:

git reset *txt
Undo git add using the git reset command through a wildcard for batch action
💡
If you want to undo everything from git add, use git reset .

Undo git add using the git rm command

If you don't like the idea of using the git reset command, then you can achieve the same behavior from the git rm command.

But unlike the previous command, here, you need to use one extra flag --cached with the git rm command:

git rm --cached <file/path>

Here, I used the wildcard to remove all the staged .txt files:

git rm --cached *.txt
Use the git rm command to undo git add command

Pretty easy. Right?

More on Git 💻

I personally find Git one of the most critical skills to have if you are a computer student especially when it serves as a resume.

So here's how to create a jaw-dropping GitHub profile for students:

7 Tips to Make a Rockstar Student Developer GitHub Profile
Time to enhance the look and feel of your boring default GitHub profile with these tips and suggestions.

Don't like GitHub? Here's a list of top GitHub alternatives for self-hosting your opensource projects:

Microsoft GitHub Alternatives to Host Your Open Source Projects
If you are looking to migrate from GitHub, here are some of the best alternatives to GitHub for hosting the source code of your open-source project.

I hope you will find this guide helpful.

Sagar Sharma