Git:如何使用“??”删除 git status -s 中列出的文件,这些是我添加到本地存储库的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7559985/
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-19 06:01:53  来源:igfitidea点击:

Git: How do I remove files listed in git status -s with "??", these are files I added to local repo?

git

提问by KRB

I have a few files in my git status -s listed as,

我的 git status -s 中有几个文件列为,

?? file1

?? filepath/file2

?? file3

?? filepath/file4

?? 文件 1

?? 文件路径/文件 2

?? 文件 3

?? 文件路径/文件 4

I have been ignoring these and going along committing, pushing, pulling, and I am at the point where there are too many of these in my status report.

我一直忽略这些并继续提交,推动,拉动,而我的状态报告中有太多这些。

I tried git rm file1. It doesn't work. It says fatal path doesn't match any files.

我试过 git rm file1。它不起作用。它说致命路径与任何文件都不匹配。

Thanks!

谢谢!

回答by dmedvinsky

These are untracked files, i.e. the files which are present in your file system, but you've never added them to your repository by git add.

这些是未跟踪的文件,即文件系统中存在的文件,但您从未通过git add.

If you don't need them, you can just rmthem. Or simply git clean -fdif you want to delete them all. If you want to do some filtering before removing them, you can do:

如果你不需要它们,你可以只需要rm它们。或者只是git clean -fd如果你想全部删除它们。如果您想在删除它们之前进行一些过滤,您可以执行以下操作:

git ls-files -o --exclude-standard | grep 'my custom filter' | xargs rm

If you want to keep those files, but want git statusto ignore them, add them to .gitignorefile. Read man gitignorefor the details.

如果您想保留这些文件,但又想git status忽略它们,请将它们添加到.gitignore文件中。阅读man gitignore详情。

回答by klaustopher

You can temporarily add those to your .gitignorefile so they won't show up in your git statusand you don't accidentally add them.

您可以暂时将这些添加到您的.gitignore文件中,这样它们就不会出现在您的文件中git status,也不会意外添加它们。

That git rm filedoesn't work because those files have not been added to your repository so there is nothing to delete

git rm file不起作用,因为这些文件尚未添加到您的存储库中,因此无法删除任何内容

回答by Matthieu Moy

These files are untracked, hence git rmdoes not work (it is the command to remove files from Git's control). You essentially have two options:

这些文件未被跟踪,因此git rm不起作用(这是从 Git 控制中删除文件的命令)。您基本上有两个选择:

  • Ignore these files. Read gitignore. You can either add the ignore patterns to a .gitignorefile that you can commit and share with your co-workers, or in a per-repo .git/info/exclude, or in a per-user .config/git/ignorefile.

  • Delete these files. Any tool you use to delete files when you're not using Git would work. Additionally, Git provides you git cleanto help you in this task.

  • 忽略这些文件。阅读gitignore。您可以将忽略模式添加到.gitignore您可以提交并与您的同事共享的文件中,或者添加到per-repo.git/info/exclude或每个用户的.config/git/ignore文件中。

  • 删除这些文件。不使用 Git 时用于删除文件的任何工具都可以使用。此外,Git 为您提供git clean以帮助您完成此任务。