Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rm several files?

Tags:

git

git-rm

How do I easily remove several files without manually typing the full paths of all of them to git rm? I have plenty of modified files I'd like to keep so removing all modified is not possible either.

And also it is possible to revert the changes of several files without manually typing git checkout -- /path/to/file?

like image 468
Tower Avatar asked Mar 02 '12 07:03

Tower


People also ask

How do I git rm everything?

In order to delete files recursively on Git, you have to use the “git rm” command with the “-r” option for recursive and specify the list of files to be deleted. This is particularly handy when you need to delete an entire directory or a subset of files inside a directory.

Does git rm actually delete the file?

git rm will not remove a file from just your working directory. (There is no option to remove a file only from the working tree and yet keep it in the index; use /bin/rm if you want to do that.)

How do git rm and rm differ from each other?

As stated above in "Why use git rm instead of rm " , git rm is actually a convenience command that combines the standard shell rm and git add to remove a file from the working directory and promote that removal to the staging index.


3 Answers

You can give wildcards to git rm.

e.g.

git rm *.c

Or you can just write down the names of all the files in another file, say filesToRemove.txt:

path/to/file.c
path/to/another/file2.c
path/to/some/other/file3.c

You can automate this:

find . -name '*.c' > filesToRemove.txt

Open the file and review the names (to make sure it's alright).

Then:

cat filesToRemove.txt | xargs git rm

Or:

for i in `cat filesToRemove.txt`; do git rm $i; done

Check the manpage for xargs for more options (esp. if it's too many files).

like image 157
Manish Avatar answered Oct 23 '22 17:10

Manish


Just delete them using any other method (Explorer, whatever), then run git add -A. As to reverting several files, you can also checkout a directory.

like image 25
Ana Betts Avatar answered Oct 23 '22 18:10

Ana Betts


I found git rm's handling of wild cards annoying. Find can do the trick in one line: find . -name '*.c' -exec git rm {} \; the {} is where the file name will be substituted. The great thing about find is it can filter on a huge variety of file attributes not just name.

like image 5
CpILL Avatar answered Oct 23 '22 19:10

CpILL