Git Shell:我们如何从未跟踪的文件中删除特定文件

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

Git Shell: How can we remove specific file from untracked files

gitshellgitignore

提问by Raju

I wish to delete a specific file from untracked files in git using shell command. but as i have searched there is a solution only for like

我希望使用 shell 命令从 git 中未跟踪的文件中删除特定文件。但正如我所搜索的,只有像这样的解决方案

-f - force, 
-d - directories too, 
-x - remove ignored files too.

let consider file as like gitignore.gitignore, Am failed whenever i tried the command git rm gitignore.gitignorefor this concern. would appreciate any expected result.

让我们将文件视为gitignore.gitignore,每当我为此问题尝试命令git rm gitignore.gitignore时,我都失败了。将不胜感激任何预期的结果。

回答by Marcus Müller

If the file isn't tracked by git, it's not git's job to remove it. Just use normal shell commands, like rminstead of git rm.

如果 git 未跟踪该文件,则 git 没有将其删除的工作。只需使用普通的 shell 命令,比如rm代替git rm.

If you want to delete alluntracked files, you could do a git clean.

如果要删除所有未跟踪的文件,可以执行git clean.

回答by Adam

Might just need a git clean -df-n is handy to double check what you're doing first.

可能只需要一个git clean -df-n 就可以方便地仔细检查您首先在做什么。

NAME
       git-clean - Remove untracked files from the working tree

OPTIONS
       -d
           Remove untracked directories in addition to untracked files. If an untracked directory is managed by a different Git repository, it is not removed by default. Use -f option twice if
           you really want to remove such a directory.

       -f, --force
           If the Git configuration variable clean.requireForce is not set to false, git clean will refuse to delete files or directories unless given -f, -n or -i. Git will refuse to delete
           directories with .git sub directory or file unless a second -f is given.

       -n, --dry-run
           Don't actually remove anything, just show what would be done.

However, if you've got uncommited changes you'd need to use git checkout -- .

但是,如果您有未提交的更改,则需要使用 git checkout -- .

回答by ARIF MAHMUD RANA

If you have any file in your working tree & you want to remove it you will use git rm

如果您的工作树中有任何文件并且想要删除它,您将使用 git rm

Straight from the doc

直接来自文档

git-rm- Remove files from the working tree and from the index

git-rm- 从工作树和索引中删除文件

Now why will you use git rminstead of rmfor removing file from your working tree

现在为什么要使用git rm而不是rm从工作树中删除文件

The answer is - If you just use rm, you will need to follow it up with git add <fileRemoved>. git rmdoes this in one step.

答案是——如果你只是使用rm,你将需要跟进它git add <fileRemoved>git rm一步完成。

You can also use git rm --cached which will remove the file from the index (staging it for deletion on the next commit), but keep your copy in the local file system.

您还可以使用 git rm --cached 从索引中删除文件(暂存以在下一次提交时删除),但将副本保留在本地文件系统中。

This part is taken from https://stackoverflow.com/a/7434558

这部分摘自https://stackoverflow.com/a/7434558

To remove untracked file you may use rmfor tracked file which is included in your source tree you will use git rm

要删除未跟踪的文件,您可以使用rm包含在源树中的跟踪文件,您将使用git rm

回答by cerebrou

You may use the interactive mode with the flag -iin the following way

您可以通过-i以下方式使用带有标志的交互模式

git clean -id`

(dis for directories as well) and then you can choose to iterate over the file and folders by pressing a, and choose what you want and do not want to delete.

d也适用于目录),然后您可以选择通过按 来遍历文件和文件夹a,并选择您想要和不想删除的内容。