# git switch Command: Change Branches in CI

> git switch changes the current branch. Reference for -c to create branches, --detach, and the safer branch-only behavior that replaces checkout in CI scripts.

Source: https://latchkey.dev/learn/command-reference/git-switch-command-reference  
Updated: 2026-06-26

git switch moves HEAD to a different branch, with a clearer, branch-only interface than checkout.

git switch was introduced to split branch switching from file restoring. In CI it makes scripts more readable and less error-prone than the overloaded git checkout.

## Common flags

- `<branch>` - switch to an existing branch
- `-c <new-branch>` / `--create` - create a new branch and switch to it
- `-C <branch>` / `--force-create` - create or reset the branch to the current commit
- `--detach` - switch to a commit in detached HEAD state
- `--discard-changes` - throw away local changes when switching

## Example

```shell
# Create a working branch for an automated commit
git switch -c "ci/update-${RUN_ID}"
# Detach onto a tag to build a release
git switch --detach "v${VERSION}"
```

## In CI

Prefer git switch over git checkout in new pipeline scripts: it only touches branches, so a typo cannot accidentally overwrite files the way checkout can. Use --detach for tags and SHAs.

## Using this in CI

CI checkouts are shallow and detached by default, which changes the answer this command gives you. Commands that read history, branch names, or tags need the checkout configured for it.

```.github/workflows/ci.yml
- uses: actions/checkout@v4
  with:
    fetch-depth: 0   # history, tags, and git describe all need this

- run: |
    git rev-parse --is-shallow-repository   # expect false
    git rev-parse --abbrev-ref HEAD          # prints HEAD when detached
```

> `git rev-parse --abbrev-ref HEAD` returns the literal string `HEAD` on a detached checkout rather than a branch name. On GitHub Actions read `github.ref_name` instead; the git command cannot know what it was checked out for.

## FAQ

### git switch Command: Change Branches in CI?

git switch was introduced to split branch switching from file restoring. In CI it makes scripts more readable and less error-prone than the overloaded git checkout.

### In CI?

Prefer git switch over git checkout in new pipeline scripts: it only touches branches, so a typo cannot accidentally overwrite files the way checkout can. Use --detach for tags and SHAs.

---

Latchkey runs CI/CD that repairs its own failures. Agent entry points: https://latchkey.dev/agent.txt, https://latchkey.dev/openapi.json, https://latchkey.dev/llms.txt
