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
git: How to ignore all present untracked files?
提问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 status
would 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 status
will explicitly ignore those files.
每次后续调用都git status
将显式忽略这些文件。
UPDATE: the above command has a minor drawback: if you don't have a .gitignore
file yet your gitignore will ignore itself! This happens because the file .gitignore
gets created before the git status --porcelain
is executed. So if you don't have a .gitignore
file 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 .gitignore
file 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 --porcelain
is used instead ofgit status --short
because 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 sed
instead of grep
and cut
, here's another way:
另一种变体为那些喜欢谁使用sed
,而不是grep
和cut
,还有一种方法:
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 .gitignore
is:
如果您想永久忽略这些文件,将它们添加到的简单方法.gitignore
是:
- Change to the root of the git tree.
git ls-files --others --exclude-standard >> .gitignore
- 切换到 git 树的根目录。
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 no
doesn't show unstaged files either. -uno
works 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, .gitignore
file 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 status
will mention the --untracked-files=no
even 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=no
option when it takes too long.
"
git status
" 建议用户--untracked=no
在时间过长时考虑使用选项。