Q13 of 40 · Git

What does `.gitignore` do, and where should it live?

GitJuniorgitgitignorefundamentalssecurityworkflow

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 repo
  • build/ — 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:

  1. Repo-level (.gitignore at root) — committed to the repo, applies to everyone
  2. Directory-level (.gitignore inside a subdirectory) — applies only within that directory
  3. 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

Knowing that .gitignore doesn't affect already-tracked files, the git rm --cached fix, and practical QA-specific patterns (test reports, allure-results, .env files). The global gitignore for personal IDE files is a quality-of-life detail.

// COMMON PITFALL

Adding a secrets file to .gitignore after it's already been committed — the secret is still in history and still tracked. You need git rm --cached AND a history rewrite (BFG or git-filter-repo) to fully purge a committed secret.