dvc gc supports --num with --all-branches
When cleaning a shared cache, run `dvc gc` with `--all-branches --num <N>` to keep data referenced by the last N commits from each branch tip. Add `--projects <path>... --force` to preserve the union across the current project and listed projects without a confirmation prompt.
dvc gc --all-branches --num 2 --projects <project-path>... --force