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

Usually, to discard changes to a file you would do:

git checkout -- <file>

What if the change I want to discard is deleting the file? The above line would give an error:

error: pathspec '<file>' did not match any file(s) known to git.

What command will restore that single file without undoing other changes?

1 Answer

0 votes
by (50.2k points)

For this question, you need to undo the effects of 

git rm <file> 


 rm <file> followed by git add -A 

To restore the file status in the index

git reset -- <file>

To check out a copy from the index

git checkout -- <file>

To undo git add <file>, the first line of the code will meet your needs, assuming you haven't committed yet.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.4k questions

32.5k answers


108k users

Browse Categories