What is `.gitignore` and what is it for?
Show answer — try answering out loud first
.gitignore is a file where you list the patterns of files and folders you want Git to ignore, meaning it won't track them or include them in commits. It's used to leave out things that shouldn't be versioned: dependencies (node_modules/), local configuration files, secrets (.env), builds, and operating system files.
Not everything in your folder should go into the repository. For example, node_modules/ is huge and can be regenerated; a .env file can contain passwords. .gitignore tells Git: "don't even look at these files."
Pattern rules:
- One line per pattern.
carpeta/ignores an entire folder.*.logignores all files with that extension.#marks a comment.!archivomakes an exception (don't ignore something that an earlier pattern was ignoring).
Important: .gitignore only affects untracked files. If a file is already being tracked by Git, adding it to .gitignore won't stop it from being tracked; you first have to untrack it.
# Example of the content of a .gitignore file:
# Dependencies
node_modules/
# Environment variables with secrets
.env
# Logs
*.log
# System files
.DS_Store
# Ignore the build folder, but NOT a specific file inside it
build/
!build/importante.txt
# If a file was ALREADY tracked, .gitignore isn't enough:
# you have to stop tracking it (without deleting it from disk)
git rm --cached .env
git commit -m "Deja de rastrear .env"
# From here on, .gitignore does ignore it
Adding a file to .gitignore expecting it to disappear from the repo, when that file was already committed. .gitignore only ignores files that Git isn't tracking yet. To stop tracking one that's already tracked, you have to use git rm --cached. Another classic mistake: pushing node_modules/ or .env because you didn't have a .gitignore from the start.
"
.gitignoreis a file where I put patterns of files and folders I want Git to ignore, so I don't version things likenode_modules,.envfiles with secrets, builds, or system files. I use patterns likecarpeta/or*.log. Something key:.gitignoreonly applies to files that Git isn't tracking yet; if something was already committed, I first have to untrack it withgit rm --cached."
You had already committed .env by mistake. Now you add it to .gitignore. Does Git stop tracking it?
See answer
No. .gitignore only ignores untracked files; .env is already being tracked. You have to run git rm --cached .env to untrack it (without deleting it from your disk) and commit that change. From there on, .gitignore will ignore it. Note: the .env will still be visible in the earlier history.