Remove Files from Staging Before Committing

Chris Achard
InstructorChris Achard
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

If you've added files to the staging area (the Index) accidentally - you can remove them using git reset.

We'll first add a file to staging, but then back it out with:

git reset HEAD filename

in order to pull it back to the working directory, so it doesn't end up in a commit when we don't want it to.

Instructor: [00:00] Now, let's add another JavaScript file called limb.js and inside of limb.js, we can say that, "This is my limb file." Then, we want to do a Git status to see that it's on tract. Let's add the limb JS file and we're about to commit.

[00:19] We have some changes to be committed, but then we maybe realize that we don't actually want to commit them. We've added it to staging, but we don't actually want to commit this limb file at all. The good news is that Git tells us how to fix it right here.

[00:33] We can do a Git reset HEAD and then the file name. Let's figure out first what this head is. If we do a Git log one line, then we can see that head is a pointer to a branch and that branch is just a pointer to the commit specified by this hash.

[00:52] If we say git reset HEAD, then give it a file, it will reset this file back to what it was in this commit, which means it won't exist. We can do git reset HEAD lib.js. Now, if we do a git status, we can see that it's untracked. It's removed from the staging area because it doesn't exist in this commit.

[01:16] Now, if we want to get rid of it entirely, we can remove lib.js. If we do a git status, then we're back to normal. We can just have our index.html and our app.js. The lib.js file is gone.