How to add a .gitignore file in Git?

I was also unsure about .gitignore at first.

You can simply create a .gitignore file in your project’s root folder using any text editor.

Just add the file patterns you want Git to ignore, like *.log or node_modules/. After that, stage and commit it with:

git add .gitignore  
git commit -m "Add .gitignore file"

Most tools like Xcode don’t create .gitignore automatically, so you usually have to add it yourself.