How to add a .gitignore file in Git?

I need to create and add rules to a .gitignore file in my project to exclude certain files from being tracked by Git.

I can’t find a .gitignore file in my project folder, and I’m not sure if tools like Xcode create it automatically.

How do I create and add a .gitignore file to my Git repository?

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.

To add a .gitignore file, just create a plain text file named .gitignore in your project root.

Inside, list all files or directories Git should ignore. For example:

Copy
Edit
*.tmp
build/
.DS_Store
Then run:


git add .gitignore  
git commit -m "Add .gitignore to exclude unwanted files"

This prevents those files from being tracked in future commits. I usually add .gitignore at the very start of a project.

I once wondered if IDEs create .gitignore for you, but most don’t.

You can manually create one by adding a file called .gitignore in your project folder.

Use a template or add rules yourself depending on what you want ignored.

After creating it, don’t forget to stage and commit it with:

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

That way, Git will stop tracking the files or folders you list there from now on.