如何忽略 Git 上的 IDE 设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24583288/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 10:10:34  来源:igfitidea点击:

How to ignore IDE settings on Git?

eclipsegit

提问by Cataclysm

I have below Git information and I would like to ignore settings of my IDE (Eclipse).

我有以下 Git 信息,我想忽略我的 IDE (Eclipse) 的设置。

modified:   myproject/.classpath
modified:   myproject/.project
modified:   myproject/.settings/com.google.gdt.eclipse.core.prefs
modified:   myproject/.settings/org.eclipse.core.resources.prefs
modified:   myproject/.settings/org.eclipse.jdt.core.prefs
modified:   myproject/.settings/org.eclipse.wst.common.component
modified:   myproject/.settings/org.eclipse.wst.common.project.facet.core.xml
modified:   myproject/.settings/org.eclipse.wst.validation.prefs

I tried the below statements in my .gitignorefile, but it doesn't work for these settings:

我在我的.gitignore文件中尝试了以下语句,但它不适用于这些设置:

.project
.classpath
.settings
*.project
*.classpath
*.settings
/.project
/.classpath
/.settings
.project/
.classpath/
.settings/
*.project/
*.classpath/
*.settings/

I am using Mac OS X and I also added global gitignore file with these settings git config --global core.excludesfile '~/.gitignore', but I'm still getting the above Git update messages when I check with git status. What am I wrong?

我正在使用 Mac OS X 并且我还使用这些设置添加了全局 gitignore 文件git config --global core.excludesfile '~/.gitignore',但是当我检查.gitignore 文件时,我仍然收到上述 Git 更新消息git status。我怎么了?

回答by VonC

If those elements were already committed, you need to remove them first:

如果这些元素已经提交,你需要先删除它们:

git rm --cached .project
git rm --cached .classpath
git rm --cached -r .settings

The --cachedoption allows them to stay in the working tree, while being recorded for deletion.
Once deleted, they will be ignored.

--cached选项允许它们保留在工作树中,同时被记录以进行删除。
一旦删除,它们将被忽略。

Once committed, the next changes will be ignored.

一旦提交,接下来的更改将被忽略。

A simple .gitignorein myproject/folder is enough:

一个简单.gitignoremyproject/文件夹就足够了:

.project
.classpath
.settings/

Note the /for .settingfolder: that will ignore everything in it.

注意/for.setting文件夹:它将忽略其中的所有内容。

回答by Eric Wang

Following is my .gitignore config for a java web project:

以下是我的 java web 项目的 .gitignore 配置:

*.class
*.swp
*.cache
*~

bin/**/*
target/**/*
build/**/*
gen/**/*

# tmp source folder
tmp/**/*


# js plugin
WebContent/js_plugin/lib/**/*

After git 1.8.2, If you want to ignore a folder named abcin any folder or subfolder, use following:

在 git 1.8.2 之后,如果要忽略任何文件夹或子文件夹中名为abc的文件夹,请使用以下命令:

**/abc/**/*

So, I suggest you upgrade your git, if it's too old.

所以,我建议你升级你的 git,如果它太旧了。

By the way, in my opinion, if team is using the same IDE, you should sync the .classpath things to git, ofcause, this is base on you have convention to name your jdk / tomcat / ... things like this. So that,once a jar is added via maven, all people get it after pull.
But, if you commit eclipse config, when push, make sure that change is useful, if not, don't commit, e.g. 2 source folder just change their line orders, in this case you can git checkout -- xxx, to ignore that change, so that won't effect other people.

顺便说一句,在我看来,如果团队使用相同的 IDE,你应该将 .classpath 的东西同步到 git,因为,这是基于你有命名你的 jdk / tomcat / ... 这样的约定。这样,一旦通过 Maven 添加了一个 jar,所有人都会在 pull 后得到它。
但是,如果您提交 eclipse 配置,在推送时,请确保更改有用,如果没有,请不要提交,例如 2 源文件夹只需更改它们的行顺序,在这种情况下,您可以 git checkout -- xxx,忽略它改变,所以不会影响其他人。