git:如何忽略所有目前未跟踪的文件? [英] git: How to ignore all present untracked files?

查看:841
本文介绍了git:如何忽略所有目前未跟踪的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方便的方法可以忽略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 文件被创建。



命令解释因为我得到很多选票你!)我想我最好解释一下这个命令:



其他变体使用 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 of git 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆