cd git-project-file
while read i; do git rm --cached $PWD/$i ; done < .gitignore
then make commits
With this you remove those files from present commits.
If you want to remove from the history, you will have to do more. So, comment below if you need to do it.
Explaination:
You said:
I have some files, for example one being error_log and some of these may have accidentally been added to git before they were added to the .gitignore file ...
so what the script does is it reads the .gitignore file line by line
so the script runs a while loop where
$i is a line
$PWD is path of present directory
so $PWD/$i is the path of the file/s in each line of .gitignore file.
It run git rm --cached $PWD/$i for each line in .gitignore.
if the file $PWD/$i is not added to git (i.e. if the file in not one of yours error_logs) it does no harm.
if it is one of your error_logs, then it removes the file from cache.
make sure you add files to .gitignore first then run the script