git:如何忽略所有目前未跟踪的文件? [英] git: How to ignore all present untracked files?
问题描述
有没有一种方便的方法可以忽略git仓库中所有未跟踪的文件和文件夹?
(我知道 .gitignore
。)
所以 git status
会再次提供一个干净的结果。
如前所述,要从状态中排除,只需使用:
git status -uno#必须是-uno,而不是-u no
您可以从项目的根目录永久忽略当前未跟踪的文件,然后启动:
git status --porcelain | grep'^ ??'| cut -c4->> .gitignore
每次后续调用 git status
将明确地忽略这些文件。
$ b 更新:上述命令有一个小缺点:如果您没有
.gitignore
档案,但你的gitignore会忽略它本身!发生这种情况是因为在执行 git status --porcelain
之前创建了文件 .gitignore
。因此,如果您没有 .gitignore
档案,我建议使用: echo$(git status --porcelain | grep'^ ??'| cut -c4-)> .gitignore
这会创建一个子shell,它在<$ c $之前完成 c> .gitignore 文件被创建。
命令解释因为我得到很多选票你!)我想我最好解释一下这个命令:
-
git status --porcelain 使用code>而不是
git status --short
,因为 manual 表示为脚本提供易于解析格式的输出,这与短输出类似,但在git中保持稳定版本和用户配置。所以我们有可分析性和稳定性; -
grep'^ ??'
只过滤以
cut -c4 -
删除前3个字符每一行,这给了我们相对于未跟踪文件的路径;
|
符号是管道,它将前一个命令的输出传递给下列命令的输入;
>>
和>
符号为重定向操作符,它将前一个命令的输出附加到文件或覆盖/创建一个新文件,分别。
其他变体使用 sed
而不是 grep
和 cut
,这是另一种方式:
git status --porcelain | sed -n -e's / ^ ?? // p'>> .gitignore
Is there a handy way to ignore all untracked files and folders in a git repository?
(I know about the .gitignore
.)
So git status
would provide a clean result again.
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.
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:
echo "$(git status --porcelain | grep '^??' | cut -c4-)" > .gitignore
This creates a subshell which completes before the .gitignore
file is created.
COMMAND EXPLANATION as 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 manual states "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.
ANOTHER VARIANT for those who prefer using sed
instead of grep
and cut
, here's another way:
git status --porcelain | sed -n -e 's/^?? //p' >> .gitignore
这篇关于git:如何忽略所有目前未跟踪的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!