Undo “git add <dir>”?

GitGit Reset

Git Problem Overview


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.

Git Solutions


Solution 1 - Git

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.

Solution 2 - Git

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.

Solution 3 - Git

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.

Solution 4 - Git

Use find and xargs:

find dir -type f | xargs git reset

Solution 5 - Git

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

enter image description here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionneoneyeView Question on Stackoverflow
Solution 1 - GitAristotle PagaltzisView Answer on Stackoverflow
Solution 2 - GitknittlView Answer on Stackoverflow
Solution 3 - Gitmrduncle1View Answer on Stackoverflow
Solution 4 - GitmoinudinView Answer on Stackoverflow
Solution 5 - GitTofan JenaView Answer on Stackoverflow