how to add gitignore file to existing repo code example

Example 1: git add gitignore

$ touch .gitignore

Example 2: apply gitignore to current commit

#First commit all changes
git rm -r --cached .
git add .
git commit -m ".gitignore updated"

Example 3: how to put files into gitignore

$ echo debug.log >> .gitignore
$ git rm --cached debug.log
rm 'debug.log'
$ git commit -m "Start ignoring debug.log"

Example 4: what is add.gitignore

The purpose of gitignore files is to ensure that certain files not tracked by git remain untracked.