Sub topic 2
Creating a .gitignore file
This chapter covers where a .gitignore file lives, how to create one, and how Git applies it. It assumes you can run git init, git add, git commit, and git status, and that you know what a working tree is. It uses only the pattern syntax shown here. The next chapters cover patterns in detail.
What the file does
A .gitignore file is a plain text file that lists patterns. Git compares untracked paths in your working tree against those patterns. A path that matches is ignored. Git leaves it out of git status output and does not stage it when you run git add ..
The file is itself a normal file. You commit it, so everyone who clones the repository gets the same rules.
Where the file lives
Git reads .gitignore files from the working tree. The usual place is the repository root, next to the .git directory. Rules in that file apply to the whole tree below it.
You can also place a .gitignore in a subdirectory. Its rules apply only to that directory and everything below it, and patterns are interpreted relative to that directory. A later chapter covers how nested files interact with the root file.
This diagram shows only the .gitignore files. Git also reads personal sources, which a later chapter covers.
Creating the file
Create the file in the repository root with any text editor, or from the terminal. Start with a small file that covers dependencies, build output, logs, and local settings.
# Dependencies
node_modules/
# Build output at the repository root only
/dist/
# Logs and local environment files
*.log
.envEach line is one pattern. A few rules about the file format:
- A line starting with
#is a comment. - Blank lines are ignored and are useful for grouping.
- A trailing slash, as in
node_modules/, makes the pattern match directories only. - A leading slash, as in
/dist/, anchors the pattern to the directory that holds the.gitignorefile. *.logmatches any file ending in.log, and.envmatches a file with exactly that name.
Seeing it work
Create some files that match the patterns and one that does not. Then check the status.
git init demo
cd demo
printf '# Dependencies\nnode_modules/\n\n# Build output at the repository root only\n/dist/\n\n# Logs and local environment files\n*.log\n.env\n' > .gitignore
mkdir node_modules
touch node_modules/lib.js debug.log .env app.js
git status --shortThe output looks like this:
?? .gitignore
?? app.jsGit lists .gitignore and app.js as untracked. It does not list node_modules/, debug.log, or .env, because patterns match them.
Now commit the rules so they are shared.
git add .gitignore app.js
git commit -m "Add .gitignore"Common mistakes
- Wrong file name. The name must be exactly
.gitignore, with the leading dot and no extension. A file namedgitignore.txtis ignored by Git as a rules file. - Trailing spaces. Git strips trailing spaces from a pattern unless you escape them with a backslash. Stray whitespace is still a common source of confusion when you copy patterns from elsewhere.
- Adding the file late. If you run
git add .before creating.gitignore, files such asnode_modules/are already staged and stay tracked. Create the file before the first commit when you can. - Expecting a retroactive effect. Adding a pattern for a file that is already committed does not remove it from the repository.
- Forgetting to commit the file. If
.gitignorestays untracked, teammates do not get your rules.
Exercise
You run git init, create build.log and main.py, and then create a .gitignore containing *.log. What does git status --short show, and what must you do so a teammate who clones the repository gets the same rule?
Show answer
It shows ?? .gitignore and ?? main.py. build.log is not listed because *.log matches it. To share the rule, stage and commit .gitignore (for example git add .gitignore then git commit -m "Add .gitignore"), because the file is only shared once it is part of the repository.
Exercise
A repository was created, git add . was run, and node_modules/ was committed. You then add node_modules/ to .gitignore. Why does Git still track the directory?
Show answer
.gitignore only affects untracked files. The files were already added to the index and committed, so Git keeps tracking them regardless of matching patterns.