Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in DevOps and Agile by (29.3k points)

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 the 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.

1 Answer

0 votes
by (50.2k points)

So, you have staged unwanted files and now you need to revert them without losing its content then you need to do:

git reset 

This command will revert your changes and unstage all the files, then those files will be ready to stage.

Thus, you can revert those unwanted files that are staged.

Note: 

Don’t use 

git reset --hard

Because this will revert the changes in the working directory, but in your scenario, it won’t delete the files but it makes changes to the working directory.

Refer: https://git-scm.com/docs/git-reset

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...