66

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

I have a .classpath file which is currently in GIT repository.

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

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 ???

Community
  • 1
  • 1
Leem.fin
  • 40,781
  • 83
  • 202
  • 354

2 Answers2

133

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

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

Platinum Azure
  • 45,269
  • 12
  • 110
  • 134
  • 1
    how to add to .gitignore , can u please be more specific ? – Leem.fin Feb 15 '12 at 16:07
  • 1
    `git help ignore` or search the web - obviously post any gitignore question once you've tried – Useless Feb 15 '12 at 16:10
  • 2
    At the top of your directory, create a file named ".gitignore" and put `.classpath` into it. You can also use wildcards to ignore files matching a certain pattern (e.g., `*.class` to ignore all compiled Java classes). You might also be able to do .gitignores in subdirectories (which then only apply to that part of the project), but I forget. See http://linux.die.net/man/5/gitignore – Platinum Azure Feb 15 '12 at 16:10
  • 1
    So, I create .gitignore file and put the file name into the file , right? – Leem.fin Feb 15 '12 at 16:13
  • 3
    Yes, that will work for a single file like `.classpath`. I would also recommend committing the `.gitignore` file so that other developers consume it and also don't commit `.classpath` by accident. – Platinum Azure Feb 15 '12 at 16:15
  • 1
    Do I need to specify the full path of the .classpath file or I only need to put the file name no matter where it is (Of course it is must under project folder) – Leem.fin Feb 15 '12 at 16:19
  • 1
    Just the name is fine-- `.gitignore` patterns automatically apply to all subdirectories. – Platinum Azure Feb 15 '12 at 16:30
18

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

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 --cached to only remove it from source control, and use .gitignore to keep it out and stop it showing in git status.

Useless
  • 64,155
  • 6
  • 88
  • 132