git rm 几个文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9529354/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:05:36  来源:igfitidea点击:

Git rm several files?

gitgit-rm

提问by Tower

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.

如何轻松删除多个文件而无需手动输入所有文件的完整路径git rm?我有很多我想保留的修改过的文件,因此也无法删除所有修改过的文件。

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

而且还可以在不手动输入的情况下还原几个文件的更改git checkout -- /path/to/file

回答by Manish

You can give wildcards to git rm.

您可以为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:

或者你可以在另一个文件中写下所有文件的名称,比如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 xargsfor more options (esp. if it's too many files).

查看联机帮助页以xargs获取更多选项(尤其是文件太多时)。

回答by Ana Betts

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.

只需使用任何其他方法(资源管理器,无论如何)删除它们,然后运行git add -A. 至于还原几个文件,你也可以检出一个目录。

回答by jmlane

On POSIX systems, you can create a shell glob that will match all desired files, you can simply pass that to git rmand 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.

在 POSIX 系统上,您可以创建一个 shell glob 来匹配所有所需的文件,您只需将其传递给git rmgit checkout --. 在 Windows 上,cmd.exe 和 PowerShell 不包括通配符和农场到应用程序(从我读过的内容来看,git 不会对文件执行此操作)。您需要使用 Windows 命令或脚本来准备文件列表并将其适当地通过管道传输到您的 git 命令以获得类似的效果。

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.

用于将文件列表传递给 shell 命令的任何策略都适用于接受文件路径的 git 命令。

回答by krngrvr09

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)

您可以删除不需要的文件并运行以下命令: git rm $(git ls-files --deleted)

回答by Kirk Powell

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

在 Windows 10 上,使用 Git Bash,从.gitignore文件结构中的位置。

git rm -r --cached some_directory/

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

我只是用它来递归地忽略整个目录。这是我的.gitignore文件中的内容:

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

回答by Thengocphan

You simply use

您只需使用

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

to remove many files match the wildcards.

删除许多与通配符匹配的文件。

回答by VonC

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

或者你可以在另一个文件中写下所有文件的名称,比如 filesToRemove.txt

That is a good approach with Git 2.26 (Q2 2020), since "git rm" and "git stash" learns the new "--pathspec-from-file" option.

这是 Git 2.26(2020 年第二季度)的好方法,因为“ git rm”和“ git stash”学习了新的“ --pathspec-from-file”选项。

So no more for i incat filesToRemove.txt; do git rm $i; done

所以没有更多的for i incat filesToRemove.txt; do git rm $i; done

A simple git rm --pathspec-from-file=filesToRemove.txtis enough.

一个简单的git rm --pathspec-from-file=filesToRemove.txt就足够了。

See commit 8a98758, commit 8c3713c, commit 3f3d806, commit b229091, commit 0093abc, commit 2b7460d, commit 5f393dc(17 Feb 2020), and commit 6a7aca6(16 Jan 2020) by Alexandr Miloslavskiy (SyntevoAlex).
(Merged by Junio C Hamano -- gitster--in commit 9b7f726, 09 Mar 2020)

提交8a98758提交8c3713c提交3f3d806提交b229091提交0093abc提交2b7460d提交5f393dc(2020年2月17日),并提交6a7aca6(2020年1月16日),由亚历山大Miloslavskiy( )SyntevoAlex
(由Junio C gitsterHamano合并-- --提交 9b7f726 中,2020 年 3 月 9 日)

rm: support the --pathspec-from-file option

Signed-off-by: Alexandr Miloslavskiy

Decisions taken for simplicity:

It is not allowed to pass pathspec in both args and file.

Adjustments were needed for if (!argc)block:

This code actually means "pathspec is not present".
Previously, pathspec could only come from commandline arguments, so testing for argcwas a valid way of testing for the presence of pathspec. But this is no longer true with --pathspec-from-file.

During the entire --pathspec-from-filestory, I tried to keep its behavior very close to giving pathspec on commandline, so that switching from one to another doesn't involve any surprises.

However, throwing usage at user in the case of empty --pathspec-from-filewould puzzle because there's nothing wrong with "usage" (that is, argc/argv array).

On the other hand, throwing usage in the old case also feels bad to me. While it's less of a puzzle, I (as user) never liked the experience of comparing my commandline to "usage", trying to spot a difference. Since it's already known what the error is, it feels a lot better to give that specific error to user.

Judging from commit 7612a1ef("git-rm: honor -nflag" 2006-06-09, git v1.4.0), it doesn't seem that showing usage in this case was important (the patch was to avoid segfault), and it doesn't fit into how other commands react to empty pathspec (see for example git addwith a custom message).

Therefore, I decided to show new error text in both cases.
In order to continue testing for error early, I moved parse_pathspec()higher. Now it happens before read_cache()/ hold_locked_index()/ setup_work_tree(), which shouldn't cause any issues.

rm: 支持 --pathspec-from-file 选项

签字人:亚历山大·米洛斯拉夫斯基

为简单起见而作出的决定:

不允许在 args 和 file 中传递 pathspec。

if (!argc)块需要调整:

这段代码实际上意味着“pathspec 不存在”。
以前,pathspec 只能来自命令行参数,因此测试 forargc是测试 pathspec 是否存在的有效方法。但这不再适用于--pathspec-from-file.

在整个--pathspec-from-file故事中,我试图保持它的行为非常接近在命令行上提供 pathspec,以便从一个切换到另一个不会引起任何意外。

但是,在为空的情况下向用户抛出用法--pathspec-from-file会令人困惑,因为“用法”(即 argc/argv 数组)没有任何问题。

另一方面,在旧案例中抛出用法对我来说也很糟糕。虽然这不是一个难题,但我(作为用户)从不喜欢将我的命令行与“使用”进行比较,试图找出差异的体验。由于已经知道错误是什么,因此将特定错误提供给用户感觉好多了。

从 commit 7612a1ef(" git-rm: Honor -nflag" 2006-06-09, git v1.4.0)来看,在这种情况下显示用法似乎并不重要(补丁是为了避免段错误),它不适合了解其他命令如何对空路径规范做出反应(参见例如git add自定义消息)。

因此,我决定在两种情况下都显示新的错误文本。
为了尽早继续测试错误,我走得parse_pathspec()更高。现在,在它发生之前read_cache()/ hold_locked_index()/ setup_work_tree(),这应该不会造成任何问题。

回答by Balman Rawat

You can simply use:

您可以简单地使用:

git add -u

回答by CpILL

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 findis it can filter on a huge variety of file attributes not just name.

我发现git rm对通配符的处理很烦人。找到可以做的伎俩在一条线: find . -name '*.c' -exec git rm {} \; {}是文件名会被取代。最棒的find是它可以过滤各种各样的文件属性,而不仅仅是名称。

回答by Mark Leighton Fisher

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

您还可以查看Cygwin,因为它在 Windows 上提供了许多 Unix/Linux/*BSD 功能。Cygwin 包括一个 Bash shell 和 find(1),以及上面提到的其他工具。(我通常在 Windows 7 上一次有 2-4 个 Cygwin 薄荷终端,因为我发现 Cygwin 很方便。)