如何将所有当前未跟踪的文件/文件夹添加到 git ignore?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15862598/
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
how to add all currently untracked files/folders to git ignore?
提问by Denis
I initialized the git
repository and made a first commit.
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.
我初始化了git
存储库并进行了第一次提交。现在,在这个目录中我运行./configure
,并./make all
使得它填充了很多额外的文件/文件夹不想跟踪。
What I would like to do, is to add all those untracked files once and for all to my gitignore. Is there any simple way to do it?
我想做的是将所有这些未跟踪的文件一劳永逸地添加到我的 gitignore 中。有什么简单的方法可以做到吗?
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.
我可以删除一些不必要的文件,例如*.o
或*.mod
通过在 .gitignore 中指定适当的行,但这并不能解决问题。
回答by mrks
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. The grep
takes only lines that start with ??
, i.e. untracked files, the cut
removes the ??
, and the rest adds it to the .gitignore
file.
说明:git status -s
为您提供状态的简短版本,没有标题。该grep
只需要这样开始的行??
,即未跟踪的文件中,cut
删除??
,剩下的将其添加到.gitignore
文件中。
回答by poolie
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.
您可能想要编辑结果以使用通配符替换重复的模式。
回答by Oskari Mantere
If your working tree is clean except for the untracked files/folders, a shorter solution is possible using awk
:
如果您的工作树除了未跟踪的文件/文件夹之外是干净的,则可以使用更短的解决方案awk
:
git status -s | awk '{print }' >> .gitignore