Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git remove all files except some files

Tags:

git

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
like image 226
Cobry Avatar asked Feb 03 '15 18:02

Cobry


People also ask

How do you remove all files except some?

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.

How do I delete all files except a specific file extension?

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.

How do I remove all files from a git repository?

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.


1 Answers

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.

like image 194
Sven Marnach Avatar answered Nov 11 '22 11:11

Sven Marnach