How to Ignore .log Files in a Git Repository

Log files (.log) are often generated by applications during development or runtime and usually contain temporary debug output, errors, or…

How to Ignore .log Files in a Git Repository
Photo by Paul Hanaoka on Unsplash

Log files (.log) are often generated by applications during development or runtime and usually contain temporary debug output, errors, or runtime events. These files can grow large and aren't meant to be versioned.

To keep your Git repository clean, it’s best to ignore .log files entirely.

Step 1: Add .log to .gitignore

Open (or create) a .gitignore file in the root of your project and add the following line:

*.log

This tells Git to ignore all .log files in all folders of your repository. If you only want to ignore .log files in a specific folder (e.g., logs/), use a path-based rule like:

/logs/*.log

Step 2: Stop Tracking Existing .log Files

If any .log files have already been committed to the repository, you need to remove them from the Git index to stop tracking them:

git rm --cached *.log 
git commit -m "Stop tracking .log files"

Step 3: Push the Changes (Optional)

To reflect these changes in the remote repository (e.g., GitHub, GitLab), push the updated state:

git push

Summary

  1. Ignore all log files: Add *.log to .gitignore
  2. Untrack already commited *.log with git rm — cached *.log
  3. Commit the change git commit -m "…"
  4. Push to remote (optional) git push

By ignoring .log files, you reduce noise in your repository, avoid unnecessary bloat, and protect potentially sensitive runtime data from being committed.