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
Using Extended Globbing and Pattern Matching Operators Also, with the ! operator, we can exclude all files we don't want glob to match during deletion. Let's look at the list of pattern matching operators: ?(pattern-list) matches at least zero and at most one occurrence.
Microsoft Windows Browse to the folder containing the files. Click the Type column heading to sort all files by the type of files. Highlight all the files you want to keep by clicking the first file type, hold down Shift , and click the last file.
Just run the rm command with the -f and -r switch to recursively remove the . git folder and all of the files and folders it contains. This Git repo remove command also allows you to delete the Git repo while allowing all of the other files and folder to remain untouched.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With