Chapter 5: Ignoring Files in Git
Sometimes you have files in your project that Git should not track. These might be:
- π Secret files like
.env - π Output/build folders like
dist/ortemp/ - π§ͺ Logs or debug files like
debug.log
# You can tell Git to ignore them using a file called .gitignore
# This is a comment
.env # Ignore env file
*.log # Ignore ALL log files
temp/ # Ignore a folder
!keep.log # But do not ignore this specific log file
# Letβs break it down with real examples π
π .gitignore Rules Explained Simply:
#β Comment line. Git will ignore this line completely.-
*.logβ Match all files that end with `.log`.
π§ Example:debug.log,error.log -
.envβ Ignore a specific file named `.env`.
π§ Often used for secrets and API keys. -
temp/β Ignore everything inside thetempfolder.
π§ Example:temp/file.txt -
!keep.logβ Do not ignorekeep.logeven if matched earlier.
π§ Useful to whitelist specific files. -
/notes.txtβ Ignorenotes.txtonly if it's in the root folder.
π§ Wonβt ignoresrc/notes.txt.
# Let's practice:
# Bonus Tip:
If you accidentally committed a file before ignoring it, use this:
git rm --cached filename
Then add the file to `.gitignore` so it won't be tracked next time.