Skip to content

Gitignore Setup Project

Beginner ~10 min

What you'll build

A repository with a comprehensive .gitignore that excludes node_modules, environment files, build output, and OS-specific files. You will verify the ignore rules work by checking git status.

Goals

  • Create a .gitignore file with common patterns
  • Understand wildcard and directory-specific patterns
  • Verify that ignored files do not appear in git status
  • Commit the .gitignore so it is shared with the team

Prerequisites

  • Git installed and basic init/add/commit workflow

Steps

  1. 1

    Set up the project

    Create a new repository and add some files that should be ignored: a node_modules folder, an .env file, and a build directory.

    Code
    mkdir gitignore-project
    cd gitignore-project
    git init
    mkdir node_modules build
    echo "secret" > .env
    echo "{}" > node_modules/lib.json
    echo "compiled" > build/app.js
    echo "console.log('hi')" > index.js

    Expected result

    A repo with index.js (to keep) and node_modules, .env, build (to ignore).

  2. 2

    Create the .gitignore file

    Write a .gitignore with patterns for dependencies, secrets, build output, and OS files. Each line is a glob pattern.

    Code
    cat > .gitignore << 'EOF'
    # Dependencies
    node_modules/
    
    # Environment variables
    .env
    *.env
    
    # Build output
    build/
    dist/
    
    # OS files
    .DS_Store
    Thumbs.db
    
    # Logs
    *.log
    EOF

    Expected result

    A .gitignore file with ignore patterns for common file types.

  3. 3

    Verify the ignore rules

    Run git status to confirm that node_modules, .env, and build/ do not appear. Only index.js and .gitignore should show as untracked.

    Code
    git status

    Expected result

    Only index.js and .gitignore appear as untracked files.

  4. 4

    Commit the .gitignore

    Stage and commit the .gitignore and index.js. The .gitignore itself should be committed so all team members share the same ignore rules.

    Code
    git add .gitignore index.js
    git commit -m "Add .gitignore and entry point"

    Expected result

    Two files committed; ignored files are excluded from the repository.

  5. 5

    Verify repository state

    Use git status and git log to confirm the repository is clean and the commit history matches expectations. Before any destructive operation, checking the current state is a Git best practice.

    Code
    git status
    # Should show: nothing to commit, working tree clean
    
    git log --oneline -5
    # Review the last 5 commits to confirm history
    git branch -v
    # List branches with last commit info

    Expected result

    git status shows a clean working tree; git log shows the expected commit history.

Try it yourself

What you learned

You created a practical .gitignore with patterns for dependencies, secrets, build output, and OS files. A good .gitignore is the first line of defense against committing sensitive data and junk files.

Related Resources

Related Lessons

Related Tools

Related Cheatsheets

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic