> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nrwl/nx/llms.txt
> Use this file to discover all available pages before exploring further.

# nx affected

> Run a target only for projects that have been changed and their dependents.

```bash theme={null}
nx affected -t <target> [options]
```

`nx affected` determines which projects are affected by recent changes in your repository—both projects with modified files and any projects that depend on them—and runs the specified target only for those projects.

<Tip>
  Use `nx affected` in CI to skip running tasks for unchanged code, reducing pipeline time and resource usage.
</Tip>

## How affected works

Nx compares two git refs (`--base` and `--head`) to find which files have changed. It then walks the project dependency graph to identify every project that could be impacted by those file changes.

* **`--base`** — the earlier commit to compare from (commonly `origin/main` or a merge-base SHA)
* **`--head`** — the later commit to compare to (defaults to `HEAD`)

If neither is provided, Nx falls back to comparing against uncommitted and untracked changes in the working tree.

## Options

### Target selection

<ParamField query="--targets" type="string" required>
  One or more targets to run for affected projects. Alias: `--target`, `-t`. Accepts comma-separated values.

  ```bash theme={null}
  nx affected -t build
  nx affected -t build,test,lint
  ```
</ParamField>

<ParamField query="--configuration" type="string">
  The named configuration to use when running the target. Alias: `-c`.
</ParamField>

### Determining affected projects

<ParamField query="--base" type="string">
  Base git ref to compare from (e.g., a branch name, tag, or commit SHA). Represents the earlier point in history.
</ParamField>

<ParamField query="--head" type="string">
  Later git ref to compare to. Defaults to `HEAD`. Requires `--base` to be set.
</ParamField>

<ParamField query="--files" type="string">
  Treat a specific list of files (comma or space separated) as changed, instead of deriving them from git. Conflicts with `--uncommitted`, `--untracked`, `--base`, and `--head`.
</ParamField>

<ParamField query="--uncommitted" type="boolean">
  Include uncommitted (staged and unstaged) file changes. Conflicts with `--files`, `--untracked`, `--base`, and `--head`.
</ParamField>

<ParamField query="--untracked" type="boolean">
  Include untracked files. Conflicts with `--files`, `--uncommitted`, `--base`, and `--head`.
</ParamField>

<ParamField query="--stdin" type="boolean">
  Read changed file paths from stdin (one per line). Useful for piping output from `git diff`. Conflicts with `--uncommitted`, `--untracked`, `--base`, and `--head`.
</ParamField>

### Filtering and scoping

<ParamField query="--exclude" type="string">
  Comma-separated list of projects to exclude from the run, even if they are affected.
</ParamField>

### Execution

<ParamField query="--parallel" type="string">
  Maximum number of parallel task processes. Defaults to `3`. Set to `false` to run serially.
</ParamField>

<ParamField query="--output-style" type="string">
  Controls how task output is displayed. Choices: `tui`, `dynamic`, `dynamic-legacy`, `static`, `stream`, `stream-without-prefixes`.
</ParamField>

<ParamField query="--nx-bail" type="boolean" default="false">
  Stop after the first failed task.
</ParamField>

<ParamField query="--nx-ignore-cycles" type="boolean" default="false">
  Ignore cycles in the task graph.
</ParamField>

<ParamField query="--skip-nx-cache" type="boolean" default="false">
  Rerun tasks even when results are available in the cache.
</ParamField>

<ParamField query="--skip-remote-cache" type="boolean" default="false">
  Disable the remote cache for this run.
</ParamField>

<ParamField query="--exclude-task-dependencies" type="boolean" default="false">
  Skip running dependent tasks first.
</ParamField>

<ParamField query="--skip-sync" type="boolean" default="false">
  Skip running sync generators associated with the tasks.
</ParamField>

<ParamField query="--batch" type="boolean">
  Run tasks in batches for executors that support batch execution.
</ParamField>

<ParamField query="--graph" type="string">
  Show the task graph instead of running it. Pass a file path to save to a file, or `stdout` to print to the terminal.
</ParamField>

<ParamField query="--runner" type="string">
  The tasks runner to use, as configured in `nx.json`.
</ParamField>

<ParamField query="--tui" type="boolean">
  Enable or disable the Nx Terminal UI. Conflicts with `--output-style`.
</ParamField>

<ParamField query="--verbose" type="boolean">
  Print additional information about the command.
</ParamField>

## Examples

<CodeGroup>
  ```bash Run build for affected projects theme={null}
  nx affected -t build
  ```

  ```bash Run multiple targets for affected projects theme={null}
  nx affected -t build test lint
  ```

  ```bash Compare against main branch (typical CI usage) theme={null}
  nx affected -t build --base=origin/main --head=HEAD
  ```

  ```bash Compare using a specific merge-base SHA theme={null}
  nx affected -t test --base=abc1234 --head=def5678
  ```

  ```bash Pipe changed files from git diff theme={null}
  git diff --name-only origin/main | nx affected -t lint --stdin
  ```

  ```bash Run with increased parallelism theme={null}
  nx affected -t build --parallel=8
  ```

  ```bash Exclude specific projects theme={null}
  nx affected -t test --exclude=e2e-app,slow-project
  ```
</CodeGroup>

## CI example

A typical GitHub Actions step that runs tasks only for affected projects:

```yaml theme={null}
- name: Run affected tasks
  run: nx affected -t build test lint --base=origin/main --head=HEAD --parallel=4
```

<Warning>
  The `--all` flag has been removed from `nx affected`. To run a target for all projects, use `nx run-many` instead.
</Warning>
