从GIT控件中删除文件

时间:2012-02-15 16:01:32

标签: git github git-svn

  

可能重复:
  git - removing a file from source control (but not from the source)

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

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

P.S。

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

2 个答案:

答案 0 :(得分:79)

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

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

答案 1 :(得分:14)

  

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

因为这就是它所说的。

$ 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.

正如Platinum Azure所说,使用git rm --cached仅将其从源代码管理中删除,并使用.gitignore将其保留,并将其停止显示在git status中。