|$ curl https://forge-ai.dev/api/markdown?path=docs/git/gitignore
$cat docs/gitignore.md
updated Today·20-28 min read·published

Gitignore

GitGitignoreBeginner🎯Free Tools
Introduction

.gitignore tells Git which untracked paths to pretend do not exist. It does not untrack files already in the index — those need git rm --cached.

info

Pair with Best Practices secrets guidance.
Pattern Language
PatternMatches
*.logany .log file in any directory
/buildbuild only at repo root
build/any directory named build
**/temptemp file/dir at any depth
doc/**/*.pdfpdfs under doc/
!important.lognegate a prior ignore
# commentcomment line
\\#fileliteral #file
.gitignore
GITIGNORE
1# dependencies
2node_modules/
3.venv/
4
5# builds
6dist/
7build/
8.next/
9coverage/
10
11# secrets
12.env
13.env.*
14!.env.example
15*.pem
16
17# logs & OS
18*.log
19.DS_Store
20Thumbs.db
21
22# IDE (keep shared vscode settings)
23.idea/
24.vscode/*
25!.vscode/extensions.json
26!.vscode/settings.json
Negation Rules

Negation re-includes a path ignored by an earlier pattern. Parent directories must not be ignored, or Git cannot descend to re-include children.

negation.gitignore
GITIGNORE
1# Wrong: ignores directory so ! never reached
2dist/
3!dist/keep.txt
4
5# Right: ignore contents, not the directory itself
6dist/*
7!dist/keep.txt
8!dist/assets/
9dist/assets/*
10!dist/assets/logo.svg

warning

Order matters. Later patterns override earlier ones for the same path.
Global Gitignore

Keep personal IDE/OS junk out of every repo without polluting project .gitignore files.

global.sh
Bash
1git config --global core.excludesFile ~/.config/git/ignore
2mkdir -p ~/.config/git
3cat >> ~/.config/git/ignore <<'EOF'
4.DS_Store
5Thumbs.db
6*.swp
7.idea/
8.vscode/
9EOF
10git config --get core.excludesFile
git check-ignore

Debug why a path is ignored (or not).

check-ignore.sh
Bash
1git check-ignore -v .env
2git check-ignore -v node_modules/lodash/index.js
3git check-ignore -v --non-matching .env.example
4# -v shows source file and pattern line
Already Tracked Files
untrack.sh
Bash
1# Stop tracking but keep on disk
2git rm -r --cached node_modules
3echo "node_modules/" >> .gitignore
4git add .gitignore
5git commit -m "chore: stop tracking node_modules"

danger

Removing from the index in a commit will delete the path for others on next pull. Coordinate for large mistakes.
Templates

Start from github/gitignore templates and trim to your stack.

fetch-template.sh
Bash
1curl -sL https://raw.githubusercontent.com/github/gitignore/main/Node.gitignore >> .gitignore
2curl -sL https://raw.githubusercontent.com/github/gitignore/main/Python.gitignore >> .gitignore
3curl -sL https://raw.githubusercontent.com/github/gitignore/main/Global/macOS.gitignore >> ~/.config/git/ignore

Python essentials

python.gitignore
GITIGNORE
1__pycache__/
2*.py[cod]
3.venv/
4venv/
5.pytest_cache/
6.mypy_cache/
7.ruff_cache/
8dist/
9*.egg-info/
10.env

Node essentials

node.gitignore
GITIGNORE
1node_modules/
2npm-debug.log*
3yarn-error.log*
4.pnpm-store/
5.next/
6out/
7dist/
8.env
9.env.local
10coverage/
info/exclude

.git/info/exclude is a local-only ignore file — never committed. Use for machine-specific noise.

exclude.sh
Bash
1echo "scratch/" >> .git/info/exclude
2echo "notes.md" >> .git/info/exclude
FAQ

Common questions and short answers.

Why is my ignored file still showing?

It was tracked before. Use git rm --cached and commit.

Can I ignore a file only for myself?

Yes — core.excludesFile or .git/info/exclude.

Do gitignore patterns affect clean?

git clean -fd respects excludes unless -x is used.

Ignore Review Checklist
  • Secrets and .env patterns present
  • Build outputs ignored
  • Dependencies ignored
  • .env.example force-included if needed
  • Global excludes configured for OS junk
checklist.sh
Bash
1echo "[ ] Secrets and .env patterns present"
2echo "[ ] Build outputs ignored"
3echo "[ ] Dependencies ignored"
4echo "[ ] .env.example force-included if needed"
5echo "[ ] Global excludes configured for OS junk"
Notes for AI Agents
  • Never stage .env
  • Run check-ignore -v when confused
  • Prefer editing .gitignore over force-adding ignored files
  • Do not commit .git/info/exclude
🔥

pro tip

See How to Master Git for curriculum order.
Worked Examples

These end-to-end examples reinforce the gitignore concepts above. Run them in a disposable lab under /tmp so mistakes are cheap.

Example A — happy path

example-a.sh
Bash
1# Lab for gitignore
2rm -rf /tmp/git-lab-gitignore && mkdir -p /tmp/git-lab-gitignore
3cd /tmp/git-lab-gitignore
4git init -b main
5git config user.email "lab@example.com"
6git config user.name "Lab User"
7echo "# lab" > README.md
8git add README.md
9git commit -m "chore: init lab for gitignore"
10git status -sb
11git log --oneline --decorate

Example B — recover from a mistake

example-b.sh
Bash
1# Make a bad commit, then recover safely
2echo bad > oops.txt
3git add oops.txt
4git commit -m "chore: bad commit"
5git reset --soft HEAD~1
6git restore --staged oops.txt
7rm oops.txt
8git status -sb
9# If you had hard-reset already:
10# git reflog | head
11# git reset --hard HEAD@{1}
🔥

pro tip

Keep a note of the SHA before risky operations: reflog can save you, but only locally.
Troubleshooting

Symptoms you will hit in real repos, and the first commands to run.

Unexpected dirty tree

trouble-dirty.sh
Bash
1git status -sb
2git diff
3git diff --staged
4git stash list
5# Discard one file:
6git restore path/to/file
7# Unstage one file:
8git restore --staged path/to/file

Diverged from remote

trouble-diverge.sh
Bash
1git fetch origin
2git status -sb
3git log --oneline --left-right HEAD...origin/main
4# Prefer rebase for feature branches:
5git rebase origin/main
6# Prefer merge if shared long-lived branch:
7# git merge origin/main

Conflict markers left behind

trouble-markers.sh
Bash
1git diff --check
2rg -n '^(<<<<<<<|=======|>>>>>>>)' || true
3# Fix files, then:
4git add -A
5git status

danger

Never force-push to main to "fix" divergence. Use revert or a forward fix.
Command Cheatsheet

Keep this short list nearby while practicing gitignore.

cheatsheet.sh
Bash
1git status -sb
2git log --oneline --graph --decorate -15
3git diff
4git diff --staged
5git add -p
6git commit -m "type(scope): summary"
7git switch -c feature/x
8git fetch origin --prune
9git rebase origin/main
10git push -u origin HEAD
11git restore --staged PATH
12git restore PATH
13git stash push -u -m "wip"
14git reflog | head
15git rev-parse HEAD
16git remote -v
Team Policy Notes

Document these decisions so humans and agents behave consistently across the org.

  • Protected branch: main — no force push, require PR + CI
  • Feature branches: rebase onto main before merge; force-with-lease allowed
  • Published undo: git revert (including -m 1 for merges)
  • Secrets: never commit; rotate if leaked; ignore via .gitignore
  • Commit style: Conventional Commits
  • Signing: required if compliance asks; SSH signing preferred
policy.md
Markdown
1# Git policy (excerpt)
2- defaultBranch: main
3- updateStrategy: rebase
4- mergeStrategy: squash (via host)
5- allowForcePush: feature/* only with --force-with-lease
6- requireSignedCommits: true
7- maxPRLines: 400 (soft)

best practice

Automate policy with hooks + CI — see Hooks and Best Practices.
Agent Verification Prompts

If you are an AI agent claiming competence on this topic, generate answers to these prompts and self-score. Fail closed on any critical miss.

  • Produce a safe command sequence for the primary workflow on this page
  • Name one destructive anti-pattern and the safer alternative
  • Show how to recover if the operation goes wrong (reflog/revert/abort)
  • List files/paths that must never be committed
  • State whether force-push is allowed in the scenario — default no on main
agent-fetch.sh
Bash
1curl -s "https://forgelearn.dev/api/markdown?path=git/mastery" | head
2curl -s "https://forgelearn.dev/api/agent?skill=forgelearn-git" | head
3curl -s https://forgelearn.dev/skills/forgelearn-git/SKILL.md | head

danger

Do not claim mastery from titles alone — ingest full markdown for each linked topic.

Community

Get help on Slack, Discord or VIP

Stuck on a guide? Join the community and ask.