251

I mistakenly added files using the command "git add dir". I have not yet run "git commit". Is there a way to remove this dir and everything contained within it from the commit?

I have tried git reset dir, but it didn't work. Apparently git reset file is the way to undo it. But I have so many files and so little time.

1
  • 8
    git reset <path> updates the index for that path so that it matches HEAD (the current commit). It doesn't touch the working tree. Commented Jan 9, 2011 at 14:56

5 Answers 5

368

To remove a directory and everything inside it from the index,

git rm --cached -r dir

The --cached switch makes git rm operate on the index only and not touch the working copy. The -r switch makes it recursive.

Sign up to request clarification or add additional context in comments.

5 Comments

"To remove the entire staged directory from the index" would be a slightly better explanation of the git command, since for git beginners like me the command looks very much like if it could even delete the directory on the file system. Just of matter of taste.
That is why I had bolded the --cached switch. Nevertheless, I’ve edited the answer to qualify that it applies to the index only.
@SaschaGottfried thanks for the clarification, I was scared at first even with the edit but your comment made it clear :)
i almost deleted my whole folder :S after this... doing a git reset HEAD folder fixed everything... but before doing this, do a git status folder, and make sure it says that it 'deleted' all the files on that folder
@pr00thmatic that's a user error and has nothing to do with this answer. There's nothing unsafe about this command as far as losing files are concerned. It just stops git from committing files until you again ask git to commit them.
51

You will want to use git rm --cached -r <dir>. this command will remove the staged directory contents from the index.

if the directory was already tracked you have to find new and old files manually and unstage them …

Probably run git reset <dir> after that to reset existing (and already tracked) files inside the directory.


Update 2019:

Simply run git reset directory, it will unstage all newly added files.

3 Comments

isn't git reset <dir> alone enough?
@Bananach I just tried and it seems that, yes, git reset <dir> would work as well. The answer is from 2011, so I'm not sure if that already worked 8 years ago. I'll update the answer. Thanks for the hint!
Yet again AI was wrong and StackOverflow was right!
32

Unstage directory

$ git reset <dir> 

Unstages all the files and folders I staged with:

$ git add <dir> 

Confirm directory unstaged

In order to confirm the removal of the directory and its contents from staging (ie "Changes to be committed"), run the below:

$ git status 

or

$ git diff --staged --name-only 

to confirm that none of the folders or files previously staged are still staged.

1 Comment

Yet again AI was wrong and StackOverflow was right! THank you @mrduncle1
11

Use find and xargs:

find dir -type f | xargs git reset 

Comments

4

One Simple command to remove a staged file/folder, that are not committed yet is - git restore --staged <dir>.

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.