Java 忽略来自 Git 的 .classpath 和 .project

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

Ignore .classpath and .project from Git

javaeclipsegitgitignore

提问by Reddy

I keep myself telling me and others not to commit .classpath and .project files and use Maven.

我一直告诉我和其他人不要提交 .classpath 和 .project 文件并使用 Maven。

Somehow, Junior developers always ignore certain rules and commits those files and it's much better to have such files for newbies who can jump and start using the code.

不知何故,初级开发人员总是忽略某些规则并提交这些文件,对于可以跳转并开始使用代码的新手来说,最好有这样的文件。

Now from myside, I would like to try/do something. When I clone the repo, I will get .classpath and .project files and certainly they get modified in my system.

现在从我的角度来看,我想尝试/做一些事情。当我克隆 repo 时,我会得到 .classpath 和 .project 文件,当然它们会在我的系统中被修改。

But I want them not to be committed and should always be ignored while synchronizing with Git. So that my changes in local system doesn't mess up with Git and Git changes of those files doesn't mess up my local files.

但我希望它们不被提交,并且在与 Git 同步时应该始终被忽略。这样我在本地系统中的更改就不会与 Git 混淆,并且这些文件的 Git 更改不会混淆我的本地文件。

How do I achieve this? Anyway to mark those files to be ignored in such a way?

我如何实现这一目标?无论如何要以这种方式标记要忽略的文件?

采纳答案by VonC

If the .projectand .classpathare already committed, then they need to be removed from the index (but not the disk)

如果.project.classpath已经提交,那么它们需要从索引中删除(但不是磁盘)

git rm --cached .project
git rm --cached .classpath

Then the .gitignorewould work (and that file can be added and shared through clones).
For instance, this gitignore.io/api/eclipsefile will then work, which does include:

然后就.gitignore可以工作了(并且可以通过克隆添加和共享该文件)。
例如,此gitignore.io/api/eclipse文件将起作用,其中包括:

# Eclipse Core      
.project

# JDT-specific (Eclipse Java Development Tools)     
.classpath

Note that you could use a "Template Directory" when cloning (make sure your users have an environment variable $GIT_TEMPLATE_DIRset to a shared folder accessible by all).
That template folder can contain an info/excludefile, with ignore rulesthat you want enforced for allrepos, including the new ones (git init) that any user would use.

请注意,您可以在克隆时使用“模板目录”(确保您的用户将环境变量$GIT_TEMPLATE_DIR设置为所有人都可以访问的共享文件夹)。
该模板文件夹可以包含一个info/exclude文件,其中包含您希望对所有存储强制执行的忽略规则,包括任何用户都会使用的新存储库 ( )。git init



As commentedby Abdollah

作为评论Abdollah

When you change the index, you need to commit the change and push it.
Then the file is removed from the repository. So the newbies cannot checkout the files .classpathand .projectfrom the repo.

当您更改索引时,您需要提交更改并推送它。
然后从存储库中删除该文件。所以,新手无法检出的文件.classpath,并.project从回购。

回答by olan

Use a .gitignore file. This allows you to ignore certain files. http://git-scm.com/docs/gitignore

使用 .gitignore 文件。这允许您忽略某些文件。http://git-scm.com/docs/gitignore

Here's an example Eclipse one, which handles your classpath and project files: https://github.com/github/gitignore/blob/master/Global/Eclipse.gitignore

这是一个 Eclipse 示例,它处理您的类路径和项目文件:https: //github.com/github/gitignore/blob/master/Global/Eclipse.gitignore

回答by Viyaan Jhiingade

Add the below lines in .gitignore and place the file inside ur project folder

在 .gitignore 中添加以下几行并将文件放在您的项目文件夹中

/target/
/.classpath
/*.project
/.settings
/*.springBeans

回答by Abdollah

The git solution for such scenarios is setting SKIP-WORKTREE BIT. Run only the following command:

此类场景的 git 解决方案是设置SKIP-WORKTREE BIT。仅运行以下命令:

git update-index --skip-worktree .classpath .gitignore

It is used when you want git to ignore changes of files that are already managed by git and exist on the index. This is a common use case for config files.

当您希望 git忽略已由 git 管理并存在于 index 上的文件更改时使用它。这是配置文件的常见用例。

Running git rm --cacheddoesn't work for the scenario mentioned in the question. If I simplify the question, it says:

运行git rm --cached不适用于问题中提到的场景。如果我简化问题,它会说:

How to have .classpathand .projecton the repo while each one can change it locally and git ignores this change?

当每个人都可以在本地更改它并且 git 忽略此更改时,如何在 repo 上拥有.classpath.project

As I commented under the accepted answer, the drawback of git rm --cachedis that it causes a change in the index, so you need to commit the change and then push it to the remote repository. As a result, .classpathand .projectwon't be available on the repo while the PO wants them to be there so anyone that clones the repo for the first time, they can use it.

正如我在接受的答案下评论的那样,缺点git rm --cached是它会导致索引发生更改,因此您需要提交更改,然后将其推送到远程存储库。作为结果,.classpath.project不会同时PO希望他们能有这么任何人克隆回购首次,就可以利用它提供的回购。

What is SKIP-WORKTREE BIT?

什么是跳过工作树位?

Based on git documentaion:

基于 git 文档:

Skip-worktree bit can be defined in one (long) sentence: When reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead. Although this bit looks similar to assume-unchanged bit, its goal is different from assume-unchanged bit's. Skip-worktree also takes precedence over assume-unchanged bit when both are set.

跳过工作树位可以用一个(长)句来定义:当读取一个条目时,如果它被标记为跳过工作树,那么 Git 会假装它的工作目录版本是最新的,而是读取索引版本。虽然这个位看起来类似于假设未更改的位,但它的目标与假设未更改的位不同。当两者都设置时,跳过工作树也优先于假设不变位。

More details is available here.

在此处获得更多详细信息。