views:

1183

answers:

1

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).
+13  A: 

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
Excellent. Does the index refer to the repository, or the stuff that is about to be checked in?
Yar
The "index" refers to a staging area that is committed to the repository on the next commit operation.
Greg Hewgill
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.
Yar
Thanks, that worked! Just putting this here for my own reference: git `rm -r --cached *.*`
Yar
@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 *.*`?
Yar
@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
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 :)
Yar