Course lesson

Remove Files from Staging Before Committing

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

Duration
2 min
Access
Free
Transcript
Retained from source evidence

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.

We can add another Javascript file to our project with touch lib.js. After doing so, we can git status to confirm it is untracked. Then, a git add lib.js will stage it for a commit. However, we might realize after adding it that we may not want to commit that file at all.

Although Git tells us how to do it in the terminal (git reset HEAD <file>...), we want to figure out what the HEAD means. With git log --oneline we find the HEAD is just a pointer to a branch and that branch is just a pointer to the commit specified by a hash on the left.

After running git reset HEAD lib.js, we can check if we successfully removed it from being staged for a commit with git status.

✏️ Edit on GitHub