I have been caught out by what appears to be a common enough problem for git newbies.
I forgot to .gitignore a particular file, and adding it to .gitignore after having committed makes no difference.
I found this page on gitready which explains how to remove a file from the repository without removing it from the working tree (by using the command git rm --cached <file>
, which works ok, except that if I then try to merge that back into another branch, the files in the working tree get deleted.
Steps to reproduce, on an empty folder:
git init
touch want.txt
touch wantnot.txt
git add .
git commit -m "Initial"
git checkout -b BranchForIgnore
git rm --cached wantnot.txt
cat > .gitignore
wantnot.txt [Ctrl-D Ctrl-D here to exit cat]
git add .
git commit -m "Ignoring file"
Up to here, everything is fine
git checkout master
git merge BranchForIgnore
At this point, my wantnot.txt files are no longer in my master, and obviously, checking out BranchForIgnore won't help either.
What to do?