# git stash Command: Shelve Changes in CI

> git stash saves uncommitted changes aside. Reference for push, pop, and --include-untracked to clean the tree temporarily during automated steps.

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

git stash records uncommitted changes and reverts the working tree to a clean state.

Stash is mostly an interactive tool, but CI sometimes uses it to set local changes aside before a fetch or rebase, then restore them. Used carefully it keeps a script idempotent.

## Common flags

- `push` - stash current changes (the default action)
- `-u` / `--include-untracked` - also stash untracked files
- `-m <msg>` - give the stash a descriptive message
- `pop` - apply the most recent stash and drop it
- `apply` - apply a stash but keep it in the list
- `list` - show saved stashes

## Example

```shell
# Set local changes aside, update, then restore them
git stash push -u -m "ci-temp"
git pull --rebase origin main
git stash pop
```

## In CI

Be cautious: git stash pop can conflict and leave the tree in a partial state, failing the job mid-script. On ephemeral runners a clean checkout is usually simpler than stashing. Reserve stash for the rare case where you must preserve generated state across a rebase.

## 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 stash Command: Shelve Changes in CI?

Stash is mostly an interactive tool, but CI sometimes uses it to set local changes aside before a fetch or rebase, then restore them. Used carefully it keeps a script idempotent.

### In CI?

Be cautious: git stash pop can conflict and leave the tree in a partial state, failing the job mid-script. On ephemeral runners a clean checkout is usually simpler than stashing. Reserve stash for the rare case where you must preserve generated state across a rebase.

---

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
