Cobry
Cobry

Reputation: 4548

git remove all files except some files

I am trying to remove all files from a 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

Upvotes: 9

Views: 9411

Answers (1)

Sven Marnach
Sven Marnach

Reputation: 602715

If you don't have any local changes in the files you want to keep, it's easiest to first remove all files, and then add the ones back you want to keep:

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

If you do have local changes, commit or stash them first.

Upvotes: 17

Related Questions