25

I usually do this:

git init
git add .
git commit .

And then I realize that it's about to add my nbproject directory, which I want excluded/ignored. Sometimes, I even check in this directory. Had I added it to .git/info/exclude before running git add., everything works fine (it's excluded).

So then I modify .git/info/exclude and then it's too late. git no longer respects changes to .git/info/exclude.

So the questions are:

  1. How can I get git to take up the changes in the exclude file in the checkin? (I tried running git add . again, which doesn't help)
  2. Let's say I check in a directory (or file) that I want excluded. What is the least number of steps to get to the state I want (with the file excluded).
Tunaki
  • 132,869
  • 46
  • 340
  • 423
Dan Rosenstark
  • 68,471
  • 58
  • 283
  • 421

1 Answers1

39

To remove a file that you have added but not committed, use a command like this:

git rm --cached file.to.remove

This will remove the file from the index, but not touch the file on disk.

To remove a file (or files) from the most recent commit, use the above git rm --cached command followed by git commit --amend.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • Excellent. Does the index refer to the repository, or the stuff that is about to be checked in? – Dan Rosenstark Jul 21 '09 at 12:25
  • 1
    The "index" refers to a staging area that is committed to the repository on the next commit operation. – Greg Hewgill Jul 21 '09 at 12:28
  • Excellent, I get it now regarding index/cache. I see you've updated your answer, but I'll still wait an hour before marking it best answer :) Thanks for your rapid and great response. – Dan Rosenstark Jul 21 '09 at 12:38
  • 2
    Thanks, that worked! Just putting this here for my own reference: git `rm -r --cached *.*` – Dan Rosenstark Jan 30 '10 at 14:54
  • @Greg Hewgill, I know this was a year ago, but: wouldn't `git reset` or `git reset --mixed` do the same thing as `git rm --cached *.*`? – Dan Rosenstark May 10 '10 at 12:04
  • 1
    @yar: `git reset` resets the file in the index to the state at HEAD; `git rm --cached` *removes* the file from the index and does not touch the file in the working tree. The difference is if you then `git commit`, after a reset Git not change the file in the repo, but after an rm, Git will remove the file from the repo. – Greg Hewgill May 10 '10 at 19:38
  • Thank you for answering that mini-question, that clears up my doubts. Now I'll have to do some trying-stuff-out to make sure I've understood it correctly :) – Dan Rosenstark May 11 '10 at 00:06