git:如何忽略所有当前未跟踪的文件?

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

git: How to ignore all present untracked files?

gitgitignore

提问by sjas

Is there a handy way to ignore all untracked files and folders in a git repository?
(I know about the .gitignore.)

有没有一种方便的方法可以忽略 git 存储库中所有未跟踪的文件和文件夹?
(我知道.gitignore。)

So git statuswould provide a clean result again.

所以git status会再次提供一个干净的结果。

回答by Diego

As already been said, to exclude from status just use:

如前所述,要从状态中排除,只需使用:

git status -uno  # must be "-uno" , not "-u no"

If you instead want to permanently ignore currently untracked files you can, from the root of your project, launch:

如果您想永久忽略当前未跟踪的文件,您可以从项目的根目录启动:

git status --porcelain | grep '^??' | cut -c4- >> .gitignore

Every subsequent call to git statuswill explicitly ignore those files.

每次后续调用都git status将显式忽略这些文件。

UPDATE: the above command has a minor drawback: if you don't have a .gitignorefile yet your gitignore will ignore itself! This happens because the file .gitignoregets created before the git status --porcelainis executed. So if you don't have a .gitignorefile yet I recommend using:

更新:上面的命令有一个小缺点:如果你还没有.gitignore文件,你的 gitignore 会忽略它自己!发生这种情况是因为文件是在执行.gitignore之前创建的git status --porcelain。因此,如果您还没有.gitignore文件,我建议您使用:

echo "$(git status --porcelain | grep '^??' | cut -c4-)" > .gitignore

This creates a subshell which completes beforethe .gitignorefile is created.

这将创建完成一个子shell之前.gitignore创建文件。

COMMAND EXPLANATIONas I'm getting a lot of votes (thank you!) I think I'd better explain the command a bit:

命令解释因为我得到了很多选票(谢谢!)我想我最好解释一下命令:

  • git status --porcelainis used instead of git status --shortbecause manualstates "Give the output in an easy-to-parse format for scripts. This is similar to the short output, but will remain stable across git versions and regardless of user configuration." So we have both the parseability and stability;
  • grep '^??'filters only the lines starting with ??, which, according to the git status manual, correspond to the untracked files;
  • cut -c4-removes the first 3 characters of every line, which gives us just the relative path to the untracked file;
  • the |symbols are pipes, which pass the output of the previous command to the input of the following command;
  • the >>and >symbols are redirect operators, which append the output of the previous command to a file or overwrites/creates a new file, respectively.
  • git status --porcelain被使用而不是git status --short因为手册指出“以易于解析的格式为脚本提供输出。这类似于短输出,但在 git 版本之间保持稳定,无论用户配置如何。” 所以我们兼具可解析性和稳定性;
  • grep '^??'仅过滤以 开头的行??,根据git status 手册,这些行对应于未跟踪的文件;
  • cut -c4-删除每一行的前 3 个字符,这只是给我们未跟踪文件的相对路径;
  • |符号是管道,这通过前面的命令为以下命令的输入的输出;
  • >>>符号是重定向运营商分别,其前一个命令的输出附加到文件或重写/创建一个新的文件,。

ANOTHER VARIANTfor those who prefer using sedinstead of grepand cut, here's another way:

另一种变体为那些喜欢谁使用sed,而不是grepcut,还有一种方法:

git status --porcelain | sed -n -e 's/^?? //p' >> .gitignore

回答by poolie

If you want to permanently ignore these files, a simple way to add them to .gitignoreis:

如果您想永久忽略这些文件,将它们添加到的简单方法.gitignore是:

  1. Change to the root of the git tree.
  2. git ls-files --others --exclude-standard >> .gitignore
  1. 切换到 git 树的根目录。
  2. git ls-files --others --exclude-standard >> .gitignore

This will enumerate all files inside untracked directories, which may or may not be what you want.

这将枚举未跟踪目录中的所有文件,这可能是您想要的,也可能不是。

回答by sjas

Found it in the manual

手册上找到

The mode parameter is used to specify the handling of untracked files. It is optional: it defaults to all, and if specified, it must be stuck to the option (e.g. -uno, but not -u no).

mode 参数用于指定对未跟踪文件的处理。它是可选的:它默认为 all,如果指定,它必须坚持选项(例如 -uno,但不是 -u no)。

git status -uno

git status -uno

回答by Jenny D

Two ways:

两种方式:

use the argument "-uno" to git-status. Here's an example:

将参数“-uno”用于 git-status。下面是一个例子:

[jenny@jenny_vmware:ft]$ git status
# On branch ft
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#       foo
nothing added to commit but untracked files present (use "git add" to track)
[jenny@jenny_vmware:ft]$ git status -uno
# On branch ft
nothing to commit (working directory clean)

Or you can add the files and directires to .gitignore, in which case they will never show up.

或者您可以将文件和指令添加到 .gitignore,在这种情况下它们将永远不会出现。

回答by football

-u nodoesn't show unstaged files either. -unoworks as desired and shows unstaged, but hides untracked.

-u no也不显示未暂存的文件。 -uno按需要工作并显示未上演,但隐藏未跟踪。

回答by vhanla

In case you are not on Unix likeOS, this would work on Windowsusing PowerShell

如果您不在 Unix 之类的操作系统上,这将适用于使用PowerShell 的Windows

git status --porcelain | ?{ $_ -match "^\?\? " }| %{$_ -replace "^\?\? ",""} | Add-Content .\.gitignore

git status --porcelain | ?{ $_ -match "^\?\? " }| %{$_ -replace "^\?\? ",""} | Add-Content .\.gitignore

However, .gitignorefile has to have a new empty line, otherwise it will append text to the last line no matter if it has content.

但是,.gitignore文件必须有一个新的空行,否则无论是否有内容,它都会将文本附加到最后一行。

This might be a better alternative:

这可能是一个更好的选择:

$gi=gc .\.gitignore;$res=git status --porcelain|?{ $_ -match "^\?\? " }|%{ $_ -replace "^\?\? ", "" }; $res=$gi+$res; $res | Out-File .\.gitignore

$gi=gc .\.gitignore;$res=git status --porcelain|?{ $_ -match "^\?\? " }|%{ $_ -replace "^\?\? ", "" }; $res=$gi+$res; $res | Out-File .\.gitignore

回答by VonC

If you have a lot ofuntracked files, and don't want to "gitignore" all of them, note that, since git 1.8.3 (April, 22d 2013), git statuswill mention the --untracked-files=noeven if you didn't add that option in the first place!

如果你有大量的未跟踪文件,并且不希望“ gitignore”所有的人,请注意,由于git的1.8.3(四月,22D 2013年)git status将提及--untracked-files=no,即使你没有在添加选项第一名!

"git status" suggests users to look into using --untracked=nooption when it takes too long.

" git status" 建议用户--untracked=no在时间过长时考虑使用选项。