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:
- Using the git reset command
- 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
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
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
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
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:
Don't like GitHub? Here's a list of top GitHub alternatives for self-hosting your opensource projects:
I hope you will find this guide helpful.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.