Ignore Files with .gitignore
Gitignore tells Git which files to never track. Secrets and logs stay out of the history.
Write the rules
Three lines hide the notes file, the secrets file, and every log file.
$ cat .gitignoreThe secrets file is hidden
Status shows only the gitignore file itself. The secrets file never appears.
$ git statusSee which rule hides it
Line 2 of the file hides the secrets file.
$ git check-ignore -v secrets.txtExample
$ git status
$ git check-ignore -v secrets.txtKey points
- The file is called .gitignore and sits at the top of the project.
- Each line names a file or a pattern to skip.
- Ignored files never show in status.
- Ignored files are never staged, even with git add .
- Secrets, passwords, and keys belong in gitignore, never in a commit.
- Commit the .gitignore file itself, so the rules travel with the project.
- Check-ignore with -v prints the rule that hides a file.
Here, status never mentions secrets.txt. Check-ignore names the rule that hides it: line 2 of .gitignore.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does .gitignore hide?