从 GIT 控件中删除文件

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

remove a file from GIT control

gitgithubgit-svn

提问by Leem.fin

Possible Duplicate:
git - removing a file from source control (but not from the source)

可能的重复:
git - 从源代码管理中删除文件(但不是从源代码中删除)

I have a .classpathfile which is currently in GIT repository.

我有一个.classpath当前在 GIT 存储库中的文件。

After I pulled from remove repository(git pull origin master). How can I remove this file from GIT control, I mean NOT to deletethis file from my computer butremove it from GIT version control. (Because this file is not needed for version control).

在我从删除存储库(git pull origin master)中提取之后。如何从 GIT 控件中删除此文件,我的意思是NOT to delete从我的计算机中删除此文件,将其从 GIT 版本控件中删除。(因为版本控制不需要此文件)。

P.S.

聚苯乙烯

I tried git rm <path-to>/.classpath, but then my whole project complains about wrong class path, why git rm delete my file instead of remove from version control ???

我试过了git rm <path-to>/.classpath,但是我的整个项目都抱怨错误的类路径,为什么 git rm 删除我的文件而不是从版本控制中删除???

回答by Platinum Azure

Use git rm --cachedto remove from the index but not the working tree.

用于git rm --cached从索引中删除,但不从工作树中删除。

After that, you should add .classpathto your .gitignore file, to prevent it from being committed again.

之后,您应该添加.classpath到您的 .gitignore 文件中,以防止它再次被提交。

回答by Useless

why git rm delete my file instead of remove from version control ???

为什么 git rm 删除我的文件而不是从版本控制中删除???

Because that's what it says it will do.

因为这就是它所说的它会做的。

$ git help rm
NAME
       git-rm - Remove files from the working tree and from the index

SYNOPSIS
       git rm [-f | --force] [-n] [-r] [--cached] [--ignore-unmatch] [--quiet]
       [--] <file>...

DESCRIPTION
       Remove files from the index, or from the working tree and the index.
       ... When --cached is given,
       the staged content has to match either the tip of the branch or the
       file on disk, allowing the file to be removed from just the index.

OPTIONS
      ...

       --cached
           Use this option to unstage and remove paths only from the index.
           Working tree files, whether modified or not, will be left alone.

As Platinum Azure says, use git rm --cachedto only remove it from source control, and use .gitignoreto keep it out and stop it showing in git status.

正如 Platinum Azure 所说,用于git rm --cached仅将其从源代码管理中删除,并用于.gitignore将其排除在外并停止在git status.