简体   繁体   English

如何将所有当前未跟踪的文件/文件夹添加到 git ignore?

[英]how to add all currently untracked files/folders to git ignore?

I initialized the git repository and made a first commit.我初始化了git存储库并进行了第一次提交。 Now, in this directory I run ./configure and ./make all so that it populates a lot of extra files/folders don't want to track.现在,在这个目录中我运行./configure./make all以便它填充很多不想跟踪的额外文件/文件夹。

What I would like to do, is to add all those untracked files once and for all to my gitignore.我想做的是将所有这些未跟踪的文件一劳永逸地添加到我的 gitignore 中。 Is there any simple way to do it?有什么简单的方法可以做到吗?

I can get rid of some unnecessary files like *.o or *.mod by specifying appropriate lines in .gitignore, but this does not solve the problem.我可以通过在 .gitignore 中指定适当的行来摆脱一些不必要的文件,如*.o*.mod ,但这并不能解决问题。

Try this:试试这个:

git status -s | grep -e "^\?\?" | cut -c 4- >> .gitignore

Explanation: git status -s gives you a short version of the status, without headers.说明: git status -s为您提供git status -s的简短版本,没有标题。 The grep takes only lines that start with ?? grep只需要以??开头的行。 , ie untracked files, the cut removes the ?? ,即未跟踪的文件, cut删除了?? , and the rest adds it to the .gitignore file. ,其余的将其添加到.gitignore文件中。

A simpler command to do this is一个更简单的命令是

git ls-files --others --exclude-standard >> .gitignore

You might want to edit the result to replace repeated patterns with wildcards.您可能想要编辑结果以使用通配符替换重复的模式。

如果您的工作树除了未跟踪的文件/文件夹之外是干净的,则可以使用awk更短的解决方案:

git status -s | awk '{print $2}' >> .gitignore

in the accepted answer, the grep part was causing me problem (ie was not filtering, instead all lines including those starting with M (modified) were shown as well), and replacing "^\\?\\?"在接受的答案中,grep 部分导致了我的问题(即没有过滤,而是显示了包括以M开头的行(已修改)在内的所有行),并替换了"^\\?\\?" with "^??""^??" solved it for me.为我解决了。 I am on win 10 and using it in msys2's bash.我在 win 10 上并在 msys2 的 bash 中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM