Git delete all files except some files

I am trying to delete all files from git branch except 1 file

# this removes all files
git rm -rf .
# what should i use to do somthing like that
git rm -rf . --except file1 file2
+4
source share
1 answer

If you do not have local changes to the files that you want to save, the easiest way is to delete all the files, and then add the ones you want to save:

git rm -rf .
git checkout HEAD -- file1 file2

If you have local changes, first make or write them down.

+8
source

All Articles