Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unstage large number of files without deleting the content

I accidentally added a lot of temporary files using git add -A

I managed to unstage the files using the following commands and managed to remove the dirty index.

git ls-files -z | xargs -0 rm -f git diff --name-only --diff-filter=D -z | xargs -0 git rm --cached 

The above commands are listed in the git help rm. But sadly, my files were also deleted on execution, even though I had given cache option. How can I clear the index without losing the content?

Also it would be helpful if someone can explain the way this pipe operation works.

like image 411
sarat Avatar asked Aug 18 '11 07:08

sarat


People also ask

How do I Unstage all files?

In order to unstage all files and directories, execute “git reset” and they will be removed from the staging area back to your working directory.

Which command Unstage the file but it preserve the file content?

Usage: git reset [file] This command unstages the file, but it preserves the file contents.


2 Answers

If you have a pristine repo (or HEAD isn't set)[1] you could simply

rm .git/index 

Of course, this will require you to re-add the files that you did want to be added.


[1] Note (as explained in the comments) this would usually only happen when the repo is brand-new ("pristine") or if no commits have been made. More technically, whenever there is no checkout or work-tree.

Just making it more clear :)

like image 34
sehe Avatar answered Sep 23 '22 22:09

sehe


git reset

If all you want is to undo an overzealous "git add" run:

git reset 

Your changes will be unstaged and ready for you to re-add as you please.


DO NOT RUN git reset --hard.

It will not only unstage your added files, but will revert any changes you made in your working directory. If you created any new files in working directory, it will not delete them though.

like image 97
tiz.io Avatar answered Sep 20 '22 22:09

tiz.io