Q13 of 40 · Git
What does `.gitignore` do, and where should it live?
Short answer
Short answer: `.gitignore` lists file patterns that Git should not track or show in git status. It should live at the repository root for project-wide rules. Files already tracked by Git are NOT ignored — use git rm --cached <file> to stop tracking them. Use .gitignore.io or GitHub's templates for language-specific starting points.
Detail
.gitignore pattern syntax:
*.log— ignore all .log files anywhere in the repobuild/— ignore the build directory (trailing slash means directory)!important.log— un-ignore this specific file (negation)tests/fixtures/generated/— ignore generated fixture files/secrets.env— ignore only at the repo root (leading slash)
Scope levels:
- Repo-level (
.gitignoreat root) — committed to the repo, applies to everyone - Directory-level (
.gitignoreinside a subdirectory) — applies only within that directory - Global (
~/.gitignore_global) — personal ignores for your environment (OS-specific files, IDE configs)
Already-tracked files: adding a file to .gitignore does NOT stop tracking it if Git is already tracking it. You must un-track it first:
git rm --cached secrets.env # remove from tracking, keep the file locally
git commit -m "chore: untrack secrets.env"
For QA repos: always ignore target/, build/, .idea/, *.iml, test-results/, allure-results/, and any file that holds credentials or API keys.
// EXAMPLE
# Example .gitignore for a Java/Maven QA project
cat .gitignore
# Build outputs
target/
build/
*.class
# IDE files (use ~/.gitignore_global for personal IDE prefs)
.idea/
*.iml
.vscode/settings.json
# Test reports and generated outputs
allure-results/
allure-report/
test-output/
karate-reports/
# Environment and secrets
.env
*.env.local
secrets.properties
# Already tracking a file that should be ignored? Un-track it:
git rm --cached src/test/resources/secrets.properties
echo "secrets.properties" >> .gitignore
git add .gitignore
git commit -m "chore: stop tracking secrets.properties"// WHAT INTERVIEWERS LOOK FOR
// COMMON PITFALL
// Related questions
What is `.gitattributes` and how do you use it to prevent line-ending and diff issues in a cross-platform QA team?
Git
A QA repo is growing uncontrollably because large binary test fixtures (video recordings, DB dumps) are committed directly. How do you solve this?
Git
What does `git add` actually do?
Git