2012-02-15 75 views
25

可能重複:
git - removing a file from source control (but not from the source)從GIT控制刪除文件

我有一個.classpath文件這是目前在Git倉庫。

當我從刪除存儲庫(git pull origin master)拉。我如何從GIT控件中刪除此文件,我的意思是NOT to delete這個文件來自我的電腦,但是將它從GIT版本控制中刪除。 (因爲這個文件不需要版本控制)。

P.S.

我試過git rm <path-to>/.classpath,但後來我的整個項目抱怨錯誤的類路徑,爲什麼git rm刪除我的文件,而不是從版本控制中刪除?

回答

51

使用git rm --cached從索引中刪除而不是工作樹。

之後,您應該將.classpath添加到.gitignore文件中,以防止它再次被提交。

+0

如何添加到.gitignore,你可以更具體嗎? – 2012-02-15 16:07:18

+1

'git help ignore'或搜索網頁 - 明顯發佈任何gitignore問題,試過一次 – Useless 2012-02-15 16:10:28

+0

在目錄頂部,創建一個名爲「.gitignore」的文件,並將'.classpath'放入其中。您還可以使用通配符忽略匹配某種模式的文件(例如,* .class'忽略所有已編譯的Java類)。你也許可以在子目錄中執行.gitignores(它只適用於項目的那部分),但我忘記了。見http://linux.die.net/man/5/gitignore – 2012-02-15 16:10:45

11

爲什麼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. 

鉑Azure的說,用git rm --cached只從源代碼控制刪除它,並使用.gitignore讓出來,並阻止它顯示git status