Git rm several files?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 11:07:47

问题


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?


回答1:


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).




回答2:


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.




回答3:


On POSIX systems, you can create a shell glob that will match all desired files, you can simply pass that to git rm and git checkout --. On Windows, cmd.exe and PowerShell do not include globbing and farm that out to the applications (git doesn't do it for files, from what I've read). You would need to use a Windows command or script to prepare a file list and pipe it to your git commands appropriately to get a similar effect.

Any strategy that you would use to pass a list of files to a shell command will work for git commands that accept file paths.




回答4:


For removing multiple files at once, you might want to checkout the answer here

You can delete the files that you don't want and run this command: git rm $(git ls-files --deleted)




回答5:


On Windows 10, using Git Bash, from the .gitignore location in your file structure.

git rm -r --cached some_directory/

I just used this to ignore a whole directory, recursively. This is what is in my .gitignore file for this:

# Use .gitignore to ignore a directory and its contents #
/some_directory/ 



回答6:


You could also check out Cygwin, as it provides much of the Unix/Linux/*BSD functionality on Windows. Cygwin includes a Bash shell and find(1), among other tools mentioned above. (I usually have 2-4 Cygwin mintty terminals up at one time on Windows 7 because I find Cygwin that handy.)




回答7:


You can simply use:

git add -u



回答8:


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.




回答9:


You simply use

find . -name '*.DS_Store' | xargs git rm

to remove many files match the wildcards.



来源:https://stackoverflow.com/questions/9529354/git-rm-several-files

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!