git – Remove files added before a commit

Question:

By mistake I added some files that I didn't want to add to a commit , I did this:

git add .

The thing is, that added all the files in the folder and there are a couple of files that I didn't want to commit because they were for my tests.

When i do this:

git status

I get:

archivo1.js
archivo2.js
archivoQueNoQuieroCommitear.js

Ask

Since I have not commit , how do I remove archivoQueNoQuieroCommitear.js from the commit?

If possible I am trying to reset the head.

The files didn't exist before so I can't checkout .

Answer:

You must remove them from the index with:

git reset <paths>

Taken from the documentation :

git reset [-q] [<tree-ish>] [--] <paths>…​

This form resets the "index" entries for all <paths> to their corresponding state in <tree-ish> (This does not affect the current "working tree" or "branch").

This means that git reset <paths> is the opposite of git add <paths>

So that you better understand what happens, what the git add command does is add an entry to the "index" with the current content of the file in the "working tree" (your working directory).

When I say add an entry to the "index", I mean that git add makes a copy of your work file to a zone known as staging or pre-production, from where the git commit command will then take the files.

These are new files so they are only in the working tree and in the index, but not in the current branch, so they don't have HEAD so you won't be able to get them from there.

git reset removes the files from the "index" and only the original will remain in your working tree. What you wanted

Scroll to Top